Home > PHP > Carriage return output!

Carriage return output!

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:
<?php
$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;
exit;
?>

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.

Advertisements
Categories: PHP
  1. July 10, 2006 at 10:54 am

    its a great blog with the code involved. keep it up.

  2. Thangarju
    July 19, 2006 at 4:58 am

    It is only Redirecting a file.. But, creation of file is important.. I expect those aso..

  3. July 19, 2006 at 5:18 am

    Yes, I agree with suggestion if you are indicating to create the content. It depends upon the data which you want to keep in the CSV file. I will try to come back with general code for that.
    Thanks!

  4. Ravi
    July 24, 2006 at 1:48 am

    Thanks
    Good work Asit 🙂

    N’Joy Life till not Wife
    rAVI

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: