Create a CSV File for a user in PHP

前端 未结 19 2105
故里飘歌
故里飘歌 2020-11-22 11:52

I have data in a MySQL database. I am sending the user a URL to get their data out as a CSV file.

I have the e-mailing of the link, MySQL query, etc. covered.

<
19条回答
  •  傲寒
    傲寒 (楼主)
    2020-11-22 12:14

    First make data as a String with comma as the delimiter (separated with ","). Something like this

    $CSV_string="No,Date,Email,Sender Name,Sender Email \n"; //making string, So "\n" is used for newLine
    
    $rand = rand(1,50); //Make a random int number between 1 to 50.
    $file ="export/export".$rand.".csv"; //For avoiding cache in the client and on the server 
                                         //side it is recommended that the file name be different.
    
    file_put_contents($file,$CSV_string);
    
    /* Or try this code if $CSV_string is an array
        fh =fopen($file, 'w');
        fputcsv($fh , $CSV_string , ","  , "\n" ); // "," is delimiter // "\n" is new line.
        fclose($fh);
    */
    

提交回复
热议问题