2011-02-27 28 views
3

讓我說我想在/css/文件夾中創建文件調用style.css。如何在服務器上創建文件?

例子:當我點擊保存按鈕腳本將創建style.css與內容

body {background:#fff;} 
a {color:#333; text-decoration:none; } 

如果服務器不能寫入我想顯示錯誤信息文件Please chmod 777 to /css/ folder

讓我知道

回答

6
$data = "body {background:#fff;} 
a {color:#333; text-decoration:none; }"; 

if (false === file_put_contents('/css/style.css', $data)) 
    echo 'Please chmod 777 to /css/ folder'; 
4

您可以使用is_writable函數來檢查文件是否可寫。

例如:

<?php 
$filename = '/path/to/css/style.css'; 
if (is_writable($filename)) { 
    echo 'The file is writable'; 
} else { 
    echo 'Please chmod 777 to /css/ folder'; 
} 
?> 
1

是功能,您可能需要使用

或使用

,如果你的fopen的文件和操作的結果是假,那麼你不能寫文件(可能是爲了權限,也許是在安全模式下UID不匹配)

file_put_contents(php5和upper)php爲你調用fopen(),fwrite()和fclose(),如果id有錯誤(你應該確保false確實是布爾值)。

0
<?php 
$filename = 'test.txt'; 
$somecontent = "Add this to the file\n"; 

// Let's make sure the file exists and is writable first. 
if (is_writable($filename)) { 

    // In our example we're opening $filename in append mode. 
    // The file pointer is at the bottom of the file hence 
    // that's where $somecontent will go when we fwrite() it. 
    if (!$handle = fopen($filename, 'a')) { 
     echo "Cannot open file ($filename)"; 
     exit; 
    } 

    // Write $somecontent to our opened file. 
    if (fwrite($handle, $somecontent) === FALSE) { 
     echo "Cannot write to file ($filename)"; 
     exit; 
    } 

    echo "Success, wrote ($somecontent) to file ($filename)"; 

    fclose($handle); 

} else { 
    echo "The file $filename is not writable"; 
} 
?> 

http://php.net/manual/en/function.fwrite.php |示例1

相關問題