2012-09-29 129 views
-1

這段代碼在PHP中有什麼問題?用PHP寫入文件

<?php 
$f = "log.txt"; 
$fh = fopen($f, 'a') or die("Can't open log file"); 
$s = "Test Line\n"; 
fwrite($f, $s); 
fclose($f); 
?> 

的log.txt文件的權限是777 但fwrite不能寫任何東西。 問題是什麼?

+0

是否有在頁面中或在錯誤日誌中的任何錯誤? – icktoofay

+1

在'$ fh'中打開,但是寫入'$ f'這是一個字符串 – Jcl

+1

順便說一句,嘗試在嘗試查找錯誤時使用命名變量的變量...使用變量名稱,例如'$ f','$ fh'和'$ s'沒有幫助 – Jcl

回答

4

嘗試......

<?php 
$f = "log.txt"; 
$fh = fopen($f, 'a') or die("Can't open log file"); 
$s = "Test Line\n"; 
fwrite($fh, $s); 
fclose($fh); 
?> 

其實你正在使用的文件名,而不是文件句柄。

3

你想了解file_put_contents

file_put_contents("log.txt", "Test Line\n", FILE_APPEND); 
+0

至少我喜歡這個:) Thx,還沒有聽說過。 –