CSV file generation error <p>I'm working on a project for a client - a wordpress plugin that creates and maintains a database of organization members. I'll note that this plugin creates a new table within the wordpress database (instead of dealing with the data as custom_post_type meta data). I've made a lot of modifications to much of the plugin, but I'm having an issue with a feature (that I've left unchanged).</p> <p>One half of this feature does a csv import and insert, and that works great. The other half of this sequence is a feature to download the contents of this table as a csv. This part works fine on my local system, but fails when running from the server. I've poured over each portion of this script and everything seems to make sense. I'm, frankly, at a loss as to why it's failing.</p> <p>The php file that contains the logic is simply linked to. The file:</p> <pre><code>&lt;?php // initiate wordpress include('../../../wp-blog-header.php'); // phpinfo(); function fputcsv4($fh, $arr) { $csv = ""; while (list($key, $val) = each($arr)) { $val = str_replace('"', '""', $val); $csv .= '"'.$val.'",'; } $csv = substr($csv, 0, -1); $csv .= "\n"; if (!@fwrite($fh, $csv)) return FALSE; } //get member info and column data $table_name = $wpdb-&gt;prefix . "member_db"; $year = date ('Y'); $members = $wpdb-&gt;get_results("SELECT * FROM ".$table_name, ARRAY_A); $columns = $wpdb-&gt;get_results("SHOW COLUMNS FROM ".$table_name, ARRAY_A); // echo 'SQL: '.$sql.', RESULT: '.$result.'&lt;br&gt;'; //output headers header("Content-type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"members.csv\""); //open output stream $output = fopen("php://output",'w'); //output column headings $data[0] = "ID"; $i = 1; foreach ($columns as $column){ //DIAG: echo '&lt;pre&gt;'; print_r($column); echo '&lt;/pre&gt;'; $field_name = ''; $words = explode("_", $column['Field']); foreach ($words as $word) $field_name .= $word.' '; if ( $column['Field'] != 'id' &amp;&amp; $column['Field'] != 'date_updated' ) { $data[$i] = ucwords($field_name); $i++; } } $data[$i] = "Date Updated"; fputcsv4($output, $data); //output data foreach ($members as $member){ // echo '&lt;pre&gt;'; print_r($member); echo '&lt;/pre&gt;'; $data[0] = $member['id']; $i = 1; foreach ($columns as $column){ //DIAG: echo '&lt;pre&gt;'; print_r($column); echo '&lt;/pre&gt;'; if ( $column['Field'] != 'id' &amp;&amp; $column['Field'] != 'date_updated' ) { $data[$i] = $member[$column['Field']]; $i++; } } $data[$i] = $member['date_updated']; //echo '&lt;pre&gt;'; print_r($data); echo '&lt;/pre&gt;'; fputcsv4($output, $data); } fclose($output); ?&gt; </code></pre> <p>So, obviously, a routine wherein a query is run, <code>$output</code> is established with <code>fopen</code>, each row is then formatted as comma delimited and <code>fwrite</code>d, and finally the file is <code>fclose</code>d where it gets pushed to a local system.</p> <p>The error that I'm getting (from the server) is </p> <pre><code>Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found. </code></pre> <p>But it clearly is getting found, its just failing. If I enable <code>phpinfo()</code> (PHP Version 5.2.17) at the top of the file, I definitely get a response - notably <code>Cannot modify header information</code> (I'm pretty sure because <code>phpinfo()</code> has already generated a header). All the expected data does get printed to the bottom of the page (after all the phpinfo diagnostics), however, so that much at least is working correctly.</p> <p>I am guessing there is something preventing the <code>fopen</code>, <code>fwrite</code>, or <code>fclose</code> functions from working properly (a server setting?), but I don't have enough experience with this to identify exactly what the problem is. </p> <p>I'll note again that this works exactly as expected in my test environment (localhost/XAMPP, netbeans).</p> <p>Any thoughts would be most appreciated. </p> <h3>update</h3> <p>Ok - spent some more time with this today. I've tried each of the suggested fixes, including @Rudu's <code>writeCSVLine</code> fix and @Fernando Costa's <code>file_put_contents()</code> recommendation. The fact is, they all work locally. Either just <code>echo</code>ing or the <code>fopen</code>,<code>fwrite</code>,<code>fclose</code> routine, doesn't matter, works great. </p> <p>What does seem to be a problem is the inclusion of the <code>wp-blog-header.php</code> at the start of the file and then the additional <code>header()</code> calls. (The path is definitely correct on the server, btw.)</p> <p>If I comment out the <code>include</code>, I get a csv file downloaded with some errors planted in it (because <code>$wpdb</code> doesn't exist. And if comment out the <code>header</code>s, I get all my data printed to the page. </p> <p>So... any ideas what could be going on here?<br> Some obvious conflict of the wordpress environment and the proper creation of a file. </p> <p>Learning a lot, but no closer to an answer... Thinking I may need to just avoid the wordpress stuff and do a manual sql query. </p>