1

i have script that should export .csv file from database. Problem is that it will save export to specific ($f) file but wont download correct file (script download empty file)

    <?php
//load the database configuration file
include '../secure/db_connect.php';

//get records from database

$sql_list = "SELECT * FROM `hakom` ORDER BY id DESC";
$sql_list_result = $mysqli->query($sql_list);



if($sql_list_result->num_rows > 0){
    $delimiter = ",";
    $filename = "members_" . date('Y-m-d') . ".csv";

    //create a file pointer
    $f = fopen('hakom_export.csv', 'w');

    //set column headers
    $fields = array('ID', 'MSISDN_');
    fputcsv($f, $fields, $delimiter);

    //output each row of the data, format line as csv and write to file pointer
    while($row = $sql_list_result->fetch_assoc()){

        $lineData = array($row['ID'], $row['MSISDN_']);
        fputcsv($f, $lineData, $delimiter);
    }

    //move back to beginning of file
    fseek($f, 0);

    //set headers to download file rather than displayed
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="' . $filename . '";');

    //output all remaining data on a file pointer
    fpassthru($f);
}
exit;

?>

i follow tutorial on this page

rtstorm
  • 339
  • 1
  • 4
  • 16
  • Have you checked for errors? Either in the log or by switching on error messages? – KIKO Software Apr 21 '18 at 08:51
  • sorry but dont know how to do that, i am new at php/sql – rtstorm Apr 21 '18 at 08:51
  • 1
    I guess you cannot write the file on server because of rights. Try wth `$f = fopen('php://memory', 'w');` instead of use `'hakom_export.csv'`. – Syscall Apr 21 '18 at 08:53
  • Switching error messages on, see: https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display?answertab=active#tab-top – KIKO Software Apr 21 '18 at 08:53
  • Thak you, i put 'php://memory' and it worked. I dont know how but before i try to use that and it give me some error in export. Now all working, thank you – rtstorm Apr 21 '18 at 08:56

2 Answers2

0

Your opening the file for write only...

$f = fopen('hakom_export.csv', 'w');

from fopen manual page...

'w' Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

Change the mode to w+

$f = fopen('hakom_export.csv', 'w+');

'w+' Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
0

Try this code.

  <?php

// output headers so that the file is downloaded rather than displayed
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=data.csv');

// create a file pointer connected to the output stream
$output = fopen('php://output', 'w');

// output the column headings
fputcsv($output, array('Column 1', 'Column 2', 'Column 3'));

// fetch the data
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
$rows = mysql_query('SELECT field1,field2,field3 FROM table');

// loop over the rows, outputting them
while ($row = mysql_fetch_assoc($rows)) fputcsv($output, $row);


?>