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.