Archive for July, 2006

Carriage return output!

July 7, 2006 4 comments

Q:- How can I produce carriage return delimited output of any file to the client side?
Ans:- One way to do it is to create the CSV file of the file or data and redirect it to client side. CSV file is Comma Separated Value, often used to exchange data between disparate applications. After downloading the file, it will automatically give the choices in which he wants to separate the data, and after selecting the option it will create the excel file.

Q:- So how one can create and redirect the CSV file to client?
Ans:- The following code will do that:
$content = (First create the file content/data, that may be through other function call)
header(“Pragma: cache”);
header(“Content-type: application/octet-stream”);
header(“Content-Disposition: attachment; filename=<filename>.csv”);
header(“Content-transfer-encoding: binary”);
header( “Last-Modified: ” . gmdate(“D, d M Y H:i:s”) . ” GMT” );
header( “Cache-Control: post-check=0, pre-check=0”, false );
header(“Content-Length: “.strlen($content));
print $content;

In this code I am talking only about to create the CSV file and redirect that to client. This is considered that the file content/data is already created.

Categories: PHP