Fputcsv in PHP will not write to file

I searched, searched and did extensive debugging, and for me life cannot understand why fputcsv does not work for me.

I can successfully open the .csv file and write to it.

My debugging proves that the array is loaded correctly and that the foreach loop is working correctly. However, the fputcsv function does not write anything at all. I deleted all lines that might cause problems, such as URLs, etc., but they still won’t write.

I am the only person with access to this environment, so I know that this is not a file lock conflict. I can create a file and write to it, so I know this is not a problem. And I get the debug output from the foreach loop, so I know that this is not a problem with an array or loop.

I have provided my code and debug log below ...

$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); 

And here is the result of Debug magazine ...

 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. 

Everything that appears in the CSV file (as you can see in the debugging code above) "This file is open and working."

Any thoughts anyone might have would be greatly appreciated!

Thanks a lot!

Route

+6
source share
1 answer

The second argument to fputcsv() should be an array, but you are passing a string because you are looping an array of strings and writing each one individually.

I suspect you just want this:

 $myfile = fopen('/blog/pdm_twitter_ouptut.csv', 'a+'); fputcsv($myfile, $posts_meta); 

If you also want to write column headings, which I think you could use because you are using an associative array, you probably want some kind of logic to look like this:

 $filePath = '/blog/pdm_twitter_ouptut.csv'; $exists = file_exists($filePath) && filesize($filePath) > 0; $myfile = fopen($filePath, 'a+'); if (!$exists) { fputcsv($myfile, array_keys($posts_meta)); } fputcsv($myfile, $posts_meta); 
+5
source

All Articles