hp2r Posted October 7, 2010 Share Posted October 7, 2010 I created this php class which recieves an sql query from flash and then produces a csv file for download. The code in its current form produces a csv file on the server. However, I want it to save on the client computer with a 'Save As' dialog box. I am testing on localhost. Here is the class: <?php class CSVExport { public function __construct() { require_once 'Zend/Date.php'; require_once 'DatabaseConnection.php'; } public function exportCSV($query) { $result = mysql_query($query); $fname = 'CSVFile.csv'; $headers = array(); $rowArray = array(); $numFields = mysql_num_fields($result); for ($i = 0; $i < $numFields; $i++) { $headers[] = mysql_field_name($result , $i); } $fp = fopen($fname, 'w'); if ($fp && $result) { header('Content-Type: application/csv'); header('Content-Disposition: inline; filename='.$fname); fputcsv($fp, $headers); while ($thisrow = mysql_fetch_row($result)) { fputcsv($fp, $thisrow); } readfile($fname); fclose($fp); } die; } } Thanks Quote Link to comment Share on other sites More sharing options...
phpchamps Posted October 7, 2010 Share Posted October 7, 2010 You will have to force download that : Below is the example code // Define the path to file$file = 'ryboe_tag_cloud.zip';if(!file){ // File doesn't exist, output error die('file not found');}else{ // Set headers header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/zip"); header("Content-Transfer-Encoding: binary"); // Read the file from disk readfile($file);} Quote Link to comment Share on other sites More sharing options...
hp2r Posted October 7, 2010 Author Share Posted October 7, 2010 Thanks for the response phpchamps. Unfortunately, it didnt work That example you gave would work if it was run in a separate php file. However, when I try to implement it within my php class, it would only save the file on the server. Could you check if I should change my class to make the headers work? Quote Link to comment Share on other sites More sharing options...
phpchamps Posted October 7, 2010 Share Posted October 7, 2010 i dont know why ur didnt work.. just modified ur script as i dont have DB and it worked for me.. find attached screenshot <?phpclass CSVExport{public function __construct(){}public function exportCSV(){ $result = array(array('name' => 'atul', 'age' => '26'), array('name' => 'viral', 'age' => '34'), array('name' => 'mahesh', 'age' => '44')); $fname = 'CSVFile.csv'; $headers = array(); $rowArray = array(); $numFields = 2; for ($i = 0; $i < $numFields; $i++) { $headers[$i]['name'] = $result[$i]['name']; $headers[$i]['age'] = $result[$i]['age']; } $fp = fopen($fname, 'w'); if ($fp && $result) { header('Content-Type: application/csv'); header('Content-Disposition: inline; filename='.$fname); fputcsv($fp, $headers); while ($thisrow = mysql_fetch_row($result)) { fputcsv($fp, $thisrow); } readfile($fname); fclose($fp); }die;}}$obj = new CSVExport();$obj->exportCSV();?> [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
hp2r Posted October 7, 2010 Author Share Posted October 7, 2010 Weird thing is that the adapted CSVExport class you created works fine when saved as a separate php file (say test.php) and then called through the browser (http://localhost/test.php) My problem is that my class is called from flash. What I could do is call an external php script from within CSVExport class which downloads the csv file from the server. Do you know how to do this? This is the external php script. It works fine when executed through the browser. It is called DownloadCSV.php and is in the same directory as CSVExport. <?php $data = 'http://localhost/CSVFile.csv'; header("Cache-Control: public"); header("Content-Description: File Transfer"); header('Content-Type: application/csv'); header('Content-Disposition: inline; filename=CSVFile.csv'); readfile($data); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.