2011-12-12 35 views
0

我提供這個目錄&文件:PHP的fwrite()報告字樣,但他們不是有

$path = $_POST['q2Path']; 
$file = "test.txt"; 
// q2Path == "C:\Users\micah\Desktop\only_dir_named_this" 

運行代碼塊:

$write_str = "TEST TEST TEST"; 
$fh = fopen($file, 'a') or die("can't open: $path\\$file"); 
$chars = fwrite($fh, $write_str) or die("can't write to: $path\\$file"); 
fclose($fh) or die("can't close: $path\\$file"); 
echo "<pre>&gt; Appended $chars characters to: $path\\$file</pre>"; 

這是瀏覽器的輸出:

追加14個字符到:C:\用戶\彌\桌面\ only_dir_named_this \ test.txt的

最後,文件是空的,'最後修改'的時間戳沒有改變。我不確定發生了什麼。我在Win7上使用ZendServer。也許有一個需要調整的php.ini設置?我從來沒有與一個簡單的文件寫你寫$path\\$file一個問題...

+0

會發生什麼,如果你試圖寫一個文件那不存在? 在PHP腳本的目錄中是否存在test.txt?因爲你沒有指定路徑,所以它可能會寫入。 –

回答

1

,但你fopen($file, 'a')

應該

$fh = fopen($path . DIRECTORY_SEPARATOR . $file, 'a') or die("can't open: $path\\$file"); 
+0

這意味着它*寫入'test.txt',但該文件與PHP腳本位於同一目錄中。 –

+0

太棒了,謝謝。當然,目錄中有很多測試文件,每個頁面都有一個字符串 - 我在過去的30分鐘裏刷新了一次。 – doub1ejack

相關問題