Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create CSV file using PHP and save it into directory

Tags:

php

I am new to PHP. I was creating a script which should create a csv file with some data from my database and save it into a directory on my server.

I somehow done it but this file is always downloading. I dont want it to download. It should just save onto a directory.

Here is the full code for your help.

Please help me on this.

<?php
$MYSQL_HOST="localhost";
$MYSQL_USERNAME="root";
$MYSQL_PASSWORD="";
$MYSQL_DATABASE="paesana";
$MYSQL_TABLE="ds_orders";
mysql_connect( "$MYSQL_HOST", "$MYSQL_USERNAME", "$MYSQL_PASSWORD" ) or die( mysql_error( ) );
mysql_select_db( "$MYSQL_DATABASE") or die( mysql_error( $conn ) );


$filename="ePay";
$csv_filename = $filename."_".date("Y-m-d_H-i",time()).".csv";

header("Content-Type: application/vnd.ms-excel");

$today="2008-12-21";
$sql = "SELECT * FROM $MYSQL_TABLE where cDate='$today'";

$result=mysql_query($sql);

if(mysql_num_rows($result)>0){

$fileContent="Beneficiary Name,Beneficiary Account No,Beneficiary Bank Code,Transaction Amount,Narration\n";
    while($data=mysql_fetch_array($result))
    {
    $fileContent.= "".$data['customer_id'].",".$data['oNum'].","."$today".",".$data['cShipService']." ".$data['cShipMethod'].",".$data['cEmail'].",".$data['ccType'].",".$data['cShipInstruct'].",".$data['cShipFname']." ".$data['cShipLname']."\n";
}


$fileContent=str_replace("\n\n","\n",$fileContent);
    echo $fileContent;
}
header("content-disposition: attachment;filename=$csv_filename"); ?> 
like image 379
user1667374 Avatar asked Sep 13 '12 02:09

user1667374


1 Answers

  1. If you don't want to download the results, get rid of the headers.

  2. After this line:

    $csv_filename = $filename."_".date("Y-m-d_H-i",time()).".csv";
    

    Add:

    $fd = fopen ($csv_filename, "w");
    
  3. Instead of:

    echo $fileContent;
    

    Use

    fputs($fd, $fileContent);
    
  4. Don't forget to close your file

    fclose($fd);
    
like image 195
Hernan Velasquez Avatar answered Oct 12 '22 02:10

Hernan Velasquez