我已經搜索和搜索,並進行了廣泛的調試,併爲我的生活找不到爲什麼fputcsv不爲我工作。在PHP中的fputcsv不會寫入文件
我可以成功打開.csv文件並寫入它。
我的調試證明數組已正確加載,並且foreach循環正常工作。但是,fputcsv函數根本沒有寫任何東西。我刪除了所有可能導致問題的字符串,例如URL等,但它仍然不會寫入。
我是唯一有權訪問此環境的人,所以我知道它不是文件鎖衝突。我可以創建該文件並寫入,因此我知道這不是權限問題。而且,我從foreach循環獲取調試輸出,所以我知道這不是數組或循環的問題。
我會提供我的代碼和下面的調試日誌...
$posts_meta = array(
'twitter_title' => $this_title,
'twitter_brandtag' => $this_brandtag,
'twitter_hashtags' => $this_hashtags,
'twitter_iterations' => $this_iteration,
'twitter_timing' => $this_timing,
'twitter_time' => $this_time,
'twitter_id' => $post_id,
);
// Debuging
file_put_contents("/blog/debug.txt", "About to write CSV file.\n", FILE_APPEND);
file_put_contents("/blog/debug.txt", print_r($posts_meta, true)."\n", FILE_APPEND);
$myfile = fopen('/blog/pdm_twitter_ouptut.csv', 'a+');
// More debugin
file_put_contents("/blog/debug.txt", "myfile handle = ".$myfile."\n", FILE_APPEND);
fwrite($myfile, "This file is open and working.\r\n");
foreach ($posts_meta as $fields){
$fresponse = fputcsv($myfile, $fields);
// A little more debugging...
file_put_contents("/blog/debug.txt", $fields."\n", FILE_APPEND);
}
fclose($myfile);
// And more debugging
file_put_contents("/blog/debug.txt", "fputcsv response = ".$fresponse."\n", FILE_APPEND);
file_put_contents("/blog/debug.txt", "Just closed CSV file.", FILE_APPEND);
這裏是生成的調試日誌...
About to write CSV file.
Array
(
[twitter_title] => World Stocks Up As US Jobs, China Exports Improve
[twitter_brandtag] => - FP test 9
[twitter_hashtags] => #Economy #Markets #Business #Investing #Stocks
[twitter_iterations] => 12
[twitter_timing] => 240
[twitter_time] => 2013-03-08 07:55:24
[twitter_id] => 11051
)
myfile handle = Resource id #548
// Print-out of $fields here...
World Stocks Up As US Jobs, China Exports Improve
- FP test 9
#Economy #Markets #Business #Investing #Stocks
12
240
2013-03-08 07:55:24
11051
fputcsv response = // Hm!? I wonder why no response code?
Just closed CSV file.
出現在.csv文件全部是(正如你在上面的調試代碼中看到的那樣)「這個文件已經打開並且正在工作。」
任何人可能有任何想法將不勝感激!
非常感謝!
旅行
您應該記錄fputcsv調用的返回值。如果成功則返回寫入的字節數,否則返回布爾值false。由於你根本沒有得到任何響應代碼,所以在循環結尾可能是一個布爾型的錯誤,它通常是不可打印的。 – 2013-03-08 18:26:59