fputcsv in PHP will not write to file
Asked Answered
G

1

8

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

Glabella answered 8/3, 2013 at 18:22 Comment(1)
You should be logging the return values of the fputcsv call. it'd return the number of bytes written if it succeeded, boolean false otherwise. since you get no response code at all, it's probably a boolean false at the end of the loop, which is generally non-printable.Battik
A
12

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);
Aetna answered 8/3, 2013 at 18:27 Comment(2)
Ahhhh, that was it! You're beautiful! Thanks so much and have a great weekend! - TripGlabella
Don't accept the answer! He doesn't need the rep! He just does this to help people!Hamiltonian

© 2022 - 2024 — McMap. All rights reserved.