I am focusing on a task for any client - a wordpress wordpress plugin that produces and keeps a database of organization people. I'll observe that this wordpress plugin produces a brand new table inside the wordpress database (rather than coping with the information as custom_publish_type meta data). I have made lots of modifications to a lot of the wordpress plugin, but I am getting an problem having a feature (that I have left unchanged).

Half of the feature does a csv import and place, which is effective. Another 1 / 2 of this sequence is really a feature to download the items in this table like a csv. This part works fine on my small local system, but fails when running in the server. I have put over each part of this script and everything appears to create sense. I am, frankly, baffled why it's failing.

The php file that consists of the logic is just associated with. The file:

<?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->prefix . "member_db";
    $year = date ('Y');
    $members = $wpdb->get_results("SELECT * FROM ".$table_name, ARRAY_A);
    $columns = $wpdb->get_results("SHOW COLUMNS FROM ".$table_name, ARRAY_A);
    // echo 'SQL: '.$sql.', RESULT: '.$result.'<br>';

    //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 '<pre>'; print_r($column); echo '</pre>';
        $field_name = '';
        $words = explode("_", $column['Field']);
        foreach ($words as $word) $field_name .= $word.' ';
        if ( $column['Field'] != 'id' && $column['Field'] != 'date_updated' ) {
            $data[$i] = ucwords($field_name);
            $i++;
        }
    }
    $data[$i] = "Date Updated";
    fputcsv4($output, $data);

    //output data
    foreach ($members as $member){
        // echo '<pre>'; print_r($member); echo '</pre>';
        $data[0] = $member['id'];
        $i = 1;
        foreach ($columns as $column){
            //DIAG: echo '<pre>'; print_r($column); echo '</pre>';
            if ( $column['Field'] != 'id' && $column['Field'] != 'date_updated' ) {
                $data[$i] = $member[$column['Field']];
                $i++;
            }
        }
        $data[$i] = $member['date_updated'];
        //echo '<pre>'; print_r($data); echo '</pre>';
        fputcsv4($output, $data); 
    }
    fclose($output);
?>

So, clearly, a routine in which a question operates, $output is made with fopen, each row will be formatted as comma delimited and fwrited, and lastly the file is fclosed where it will get pressed to some local system.

The mistake that I am getting (in the server) is

Error 6 (net::ERR_FILE_NOT_FOUND): The file or directory could not be found.

However it clearly gets found, its just failing. Basically enable phpinfo() (PHP Version 5.2.17) towards the top of the file, I certainly obtain a response - particularly Cannot modify header information (I am confident because phpinfo() has produced a header). All of the expected data does get printed to the foot of the page (in the end the phpinfo diagnostics), however, to ensure that much a minimum of is working properly.

I'm speculating there's something stopping the fopen, fwrite, or fclose functions from working correctly (a server setting?), however i do not have sufficient knowledge about this to recognize just what the issue is.

I'll note again this works just as expected during my test atmosphere (localhost/XAMPP, netbeans).

Any ideas could be most appreciated.

update

Ok - spent more time with this particular today. I have attempted each one of the recommended fixes, including @Rudu's writeCSVLine fix and @Fernando Costa's file_put_contents() recommendation. The truth is, all of them work in your area. Either just echoing or even the fopen,fwrite,fclose routine, does not matter, is effective.

Exactly what does appear to become a issue is the inclusion from the wp-blog-header.php at the beginning of the file and so the additional header() calls. (The road is certainly correct around the server, btw.)

Basically comment the include, I recieve a csv file downloaded with a few errors grown inside it (because $wpdb does not exist. And when comment the headers, I recieve my data printed towards the page.

So... any ideas what is happening here?
Some apparent conflict from the wordpress atmosphere and also the proper development of personal files.

Learning a great deal, but no nearer to a solution... Thinking I might need to just steer clear of the wordpress stuff and perform a manual sql query.