Posted by: prajapatinilesh | October 29, 2008

Array to csv downloadable

<?php
function export_array_to_csv($inarray)  {
while (list ($key1, $val1) = each ($inarray))  {
while (list ($key, $val) = each ($val1))  {
if (is_numeric($val))  {
$sendback .= $val.”,”; //integer value
}else{
$sendback .= “\””. $val .”\”,”; //string value
}//fi
}//end of while
$sendback = substr($sendback, 0, -1); //chop last ,
$sendback .= “\n”;
}//End of while
return ($sendback);
}// end function

function send_file_to_client($filename, $data) {
header(“Content-type: application/ofx”);
header(“Content-Disposition: attachment; filename=$filename”);
echo $data;
};

function send_file_to_client_2($filename, $data) {
// required for IE, otherwise Content-disposition is ignored
if(ini_get(‘zlib.output_compression’))
ini_set(‘zlib.output_compression’, ‘Off’);

$file_extension = strtolower(substr(strrchr($filename,”.”),1));

//Start condition switch( $file_extension )
switch( $file_extension )
{
case “csv”: $ctype=”application/force-download”;break;
//case “csv”: $ctype=”application/octet-stream”;break;
//case “pdf”: $ctype=”application/pdf”; break;
//case “exe”: $ctype=”application/octet-stream”; break;
//case “zip”: $ctype=”application/zip”; break;
//case “doc”: $ctype=”application/msword”; break;
//case “xls”: $ctype=”application/vnd.ms-excel”; break;
//case “ppt”: $ctype=”application/vnd.ms-powerpoint”; break;
//case “gif”: $ctype=”image/gif”; break;
//case “png”: $ctype=”image/png”; break;
//case “jpeg”:
//case “jpg”: $ctype=”image/jpg”; break;
//default: $ctype=”application/force-download”;
default: echo “<html><title>This file cannot be downloaded</title> <body>ERROR: This file cannot be downloaded</body></html>”;exit;
}//End condition switch( $file_extension )

header(“Pragma: public”); // required
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Cache-Control: private”,false); // required for certain browsers
header(“Content-Type: $ctype; charset=utf-8”);
header(“Content-Disposition: attachment; filename=\””.basename($filename).”\”;” );
header(“Content-Transfer-Encoding: binary”);

echo $dataVal;
exit;
}

//How to use it:
$dataArr[0][0] = “value1”;
$dataArr[0][1] = “value2”;
$dataArr[1][0] = “value3”;
$dataArr[1][1] = “123123123”;
$dataArr[2][0] = “123”;
$dataArr[2][1] = “value4”;

send_file_to_client(“csvdatafile.csv”, export_array_to_csv($dataArr));
?>

Advertisements

Responses

  1. To much usefull!!!
    Thank you very much from Brazil!

  2. Hey ok.. thanks to you also that you added comment inside article.. I like Brazil country and thanks from india to all brazil people… 🙂


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

Categories

%d bloggers like this: