Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

query results into csv file 1

Status
Not open for further replies.

cruel

Programmer
Aug 6, 2001
131
I looked through FAQ and could not find anything, though the internet has lot of discussion about it.

Somone suggested the following, nice of it is it will pop out something and ask you where and what to save it to:
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=$filename.csv");
header("Pragma: no-cache");
header("Expires: 0");

but I never could save the carriage return, in fact, the comma does not even come out right. It is lumped in one cell.

So I use fwrite. I need to design my own pop out thing though. However, what in html would allow me to let user choose a folder (it has a browse for file function in <input type="file"...>, but nothing about a folder).

what to suggest?
 
i dont see any code that outputs the content of the csv to the user.

>>what in html would allow me to let user choose a folder
as to where he has to save the CSV file? that would not be possible...

Known is handfull, Unknown is worldfull
 
@cruel

this should not be problematic.
but can you help further?
are you having problems with the creation of the csv file or is it the delivery to the user?

for help with the csv file, please let us know what version of php you are using. the reason is that in version 5.1.0RC1 there is a function fputcsv which will help (remember to toggle the autodetectlineendings flag if you are on a mac). if your version is lower then you do need to use fwrite. it's really straightforward so if you are having problems post your code so we can help.

for delivery of the file to the user, the method you are using seems fine. you should include the filesize in the headers though.
 
I am able to create csv file with fwrite, but I could not figure out a way to allow user to select a folder. However, the header(content-type..." stuff can do that, but it does not go with fwrite, because fwrite does not display the results in the web page, I had to use printf or echo. However, printf or echo will make carriage return work for the page source, as we know, the html "newline character" is <br />.

And I am use PHP4.0, I prefer realibility over more powerful feature.
 
I just realized that use the header function, the saved CSV file comes with the html code that would be hidden when you open it in excel.

If html adds something <input type="folder" ...> similar to <input type="file" ...>, all the problems would be solved. Am I missing something here why a folder is not a type of file?
 
i'm really confused by what you are saying. i think you may have the wrong end of the stick somewhere.

try the following as an example. this code shows you both how to avoid any filesystem interaction (the csv creation is done in memory) and how to use the filesystem. just copy and paste it into a file. make sure there is NOTHING else in the file.

when you click a download link in most browsers the user gets presented with some form of dialog to tell the browser where to save the file.

Code:
<?
//dummy some database data
$row[] = array("name"=>"apples", "type"=>"fruit");
$row[] = array("name"=>"bananas", "type"=>"fruit");
$row[] = array("name"=>"carrots", "type"=>"vegetable");

//create the csv string
//use the heredoc syntax for ease
$action = isset($_GET['method']) ? $_GET['method'] : "";
switch ($action):

	case "inline":
		$csvstring= "";
		foreach ($row as $val):
		  $csvstring .= <<<STRING
"{$val['name']}","{$val['type']}"\r\n
STRING;
		endforeach;
		header('Content-type: application/octet');
		header('Content-Disposition: attachment; filename="cruels csv file.csv"');
		echo $csvstring;
		break;
	
	case "filesystem":
		$ftemp ="temporary csv file.csv";
		$fh = @fopen($ftemp, "w") or die("cannot open file for writing. Make sure the permissions are set ok for php to write to this file");
		foreach ($row as $val): 
			$csvstring = <<<STRING
"{$val['name']}","{$val['type']}"\r\n
STRING;
			fwrite($fh, $csvstring);
		endforeach;
		fclose($fh);
//and now serve the file
		header('Content-type: application/octet');
		header('Content-Disposition: attachment; filename="cruels csv file.csv"');
		readfile($ftemp);
		unlink($ftemp); //this deletes the file
		break;
	default:
		echo <<<S
Click <a href="{$_SERVER['PHP_SELF']}?method=inline">here</a> to use the inline method of csv creation and delivery <br/>
Click <a href="{$_SERVER['PHP_SELF']}?method=filesystem">here</a>to use the filesystem method of csv creation and delivery
S;
	endswitch;
	exit();
?>
 
I looked at your code, did not see essential difference. I used: echo join(",", $data_record) . "\n";

However, the tip that solved the problem was what you suggested in the message "nothing else". I had the html stuff in and they came along with the data in csv, those html codes were hidden when you open the file in excel (isn't Microsoft smart?) but visible in notepad. What infinitely worse is, because of these html code in csv file, excel does not how to parse the file and all the data becomes one record.

Thanks JPadie.
 
ah yes - an old chestnut.

because of this kind of thing i tend to keep the doctype declaration in a separate function or file and include it when ever i am going to output data. otherwise inevitably when i come back to code a few months later i forget exactly how things were done and find myself sending headers at inappropriate times.
 
ah yes - an old chestnut.

because of this kind of thing i tend to keep the doctype declaration in a separate function or file and include it when ever i am going to output data. otherwise inevitably when i come back to code a few months later i forget exactly how things were done and find myself sending headers at inappropriate times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top