Note that there are some explanatory texts on larger screens.

plurals
  1. POfputcsv writing to blank file
    primarykey
    data
    text
    <p>I am having some trouble exporting a CSV file using fputcsv. The file downloads but it is blank. I have tried exporting to xls and it works perfectly but I wanted to use csv with custom field headers as shown in code below. I am not sure if I am doing something wrong or if I need to have any php.ini changes done. I know I have fopen enabled but the file is about 6.5mb of database information.</p> <p>Thanks in advance!</p> <pre><code>$colnames = array( 'forDepts' =&gt; "Member No.", 'clientId' =&gt; "Date joined", 'createdOn' =&gt; "Title", 'createdBy' =&gt; "First name", 'description' =&gt; "Notes" ); function map_colnames($input) { global $colnames; return isset($colnames[$input]) ? $colnames[$input] : $input; } function cleanData(&amp;$str) { if($str == 't') $str = 'TRUE'; if($str == 'f') $str = 'FALSE'; if(preg_match("/^0/", $str) || preg_match("/^\+?\d{8,}$/", $str) || preg_match("/^\d{4}.\d{1,2}.\d{1,2}/", $str)) { $str = "'$str"; } if(strstr($str, '"')) $str = '"' . str_replace('"', '""', $str) . '"'; $str = mb_convert_encoding($str, 'UTF-16LE', 'UTF-8'); } // filename for download $filename = "website_data_" . date('Ymd') . ".csv"; header("Content-Disposition: attachment; filename=\"$filename\""); header("Content-Type: text/csv; charset=UTF-16LE"); $out = fopen("php://output", 'w'); $flag = false; $result = mysql_query("SELECT forDepts, clientId, createdOn, createdBy, description FROM notes") or die('Query failed!'); while(false !== ($row = mysql_fetch_assoc($result))) { if(!$flag) { // display field/column names as first row fputcsv($out, array_keys($row), ',', '"'); $flag = true; } array_walk($row, 'cleanData'); fputcsv($out, array_values($row), ',', '"'); } fclose($out); exit; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload