Jump to content

CSV export as download


hp2r

Recommended Posts

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

Link to comment
Share on other sites

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);}

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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);
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.