fputcsv in PHP will not write to file

user2149399 picture user2149399 · Mar 8, 2013 · Viewed 10.7k times · Source

I've have searched and searched and done extensive debugging and for the life of me cannot figure out why fputcsv is not working for me.

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

My debugging proves that the array is properly loaded and that the foreach loop is working correctly. However, the fputcsv function fails to write anything at all. I have removed all strings that I though may cause a problem such as URLs etc, but it still will not write.

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

I'll provide 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 resulting Debug log...

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.

All that appears in the .csv file is (as you can see in the debug code above) "This file is open and working."

Any thoughts anyone may have would be greatly appreciated!

Thanks so much!!!

Trip

Answer

DaveRandom picture DaveRandom · Mar 8, 2013

The second argument to fputcsv() should be an array, but you are passing in 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 want to write column headers as well, which I guess you might because you are using an associative array, you probably want some logic more 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);