如何上传使用php创建的csv文件?

如何上传使用php创建的csv文件?

问题描述:

I had write this code but it only generate csv file but not upload in directory.

$file = 'export';
    $csv_output .= "CustID,CustName,CustMobile,CustEmail
";

$sql = "select CustID,CustName,CustMobile,CustEmail from dep_customer_details LIMIT 0,10";
$res = mysql_query($sql);
while ($row = mysql_fetch_row($res)) {
 for ($j=0;$j<$res;$j++) {
  $csv_output .= $row[$j].", ";
 }
 $csv_output .= "
";
}

$filename = $file."_".date("Y-m-d_H-i",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header("Content-disposition: filename=".$filename.".csv");
print $csv_output;


$uploaddir = dirname(__FILE__) .'/csv/';
$uploadfile = $uploaddir.$filename;
move_uploaded_file($filename,$uploadfile);

This code will create a file with export_2013-09-18_10-41.csv for example (as per time of execution on my end) which saves it inside a sub-folder called csv.

However this is only an example using Mark Baker's comment file_put_contents($uploadfile,$csv_output); and not using headers.

That can be implemented afterwards as an attachment. You will need to find a function for it yourself.

<?php

$file = 'export';
$csv_output = "CustID,CustName,CustMobile,CustEmail
";

// Commented out your DB code for you to test 
// my example with above $csv_output fields only.
/*
$sql = "select CustID,CustName,CustMobile,CustEmail from dep_customer_details LIMIT 0,10";
$res = mysql_query($sql);
while ($row = mysql_fetch_row($res)) {
 for ($j=0;$j<$res;$j++) {
  $csv_output .= $row[$j].", ";
 }
 $csv_output .= "
";
}
*/

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

$uploaddir = dirname(__FILE__) .'/csv/';
$uploadfile = $uploaddir.$filename.".".csv;

file_put_contents($uploadfile,$csv_output);

print $csv_output;

// Mail function and headers for file attachment
// can be implemented in this section afterwards

?>

As Mark Baker also stated, "As the file hasn't been uploaded from a web browser but is being generated by your script on the server...", move_uploaded_file() is usually executed using a form along with other directives in order to move a created file into a folder/sub-folder.

Therefore, the use of file_put_contents is more suitable for this type of application because it will save to file and not upload, which is not what you need.

More information on both functions can be found on the PHP.net Website.

You do not have to set these headers:

header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv".date("Y-m-d").".csv");
header("Content-disposition: filename=".$filename.".csv");

Because they let the client download the file and possibly save it on his harddisk. You want to save it on your server's harddisk.

You also do not need to move the uploaded file

move_uploaded_file($filename, $uploadfile);

because there is no file (to be) uploaded by the client.

The only thing to do is the following:

  • grab the csv contents from the database;
  • save them to a file (still server-side);

So here is the code:

$file = "export";
$csv_output .= "CustID,CustName,CustMobile,CustEmail
";

$sql = "select CustID,CustName,CustMobile,CustEmail from dep_customer_details LIMIT 0,10";
$res = mysql_query($sql);
while ($row = mysql_fetch_row($res)) {
    for ($j = 0; $j < $res; $j++) {
        $csv_output .= $row[$j].", ";
    }
    $csv_output .= "
";
}

$uploaddir = dirname(__FILE__)."/csv/";
$uploadfile = $uploaddir.$filename;
file_put_contents($uploadfile, $csv_output); // Just like Mark Baker mentioned

Then you can read the file contents of the csv file and put them into the mail contents.

But since you only want to mail the CSV file, you can skip the whole process of storing the file on the server and reading it. That's because you can just put $csv_output somewhere in the mail contents.

How to do that, depends on how you are sending the mails (are you using PHPMailer, for example). See the manual (and the comments!) for more details on how to do that.

I also suggest you search for tutorials on the subject.