Creating downloadable CSV files using PHP OR PHP create csv file and force download


 
How to download csv file in php

CSV (comma-separated values) is the most widely supported format for transferring tabular data between applications. The ability to export data in CSV format is a useful feature for many programs, and is becoming increasingly common in web applications. This page explains how to use PHP to create CSV files, and how to ensure that your visitor’s browser offers to download the file instead of displaying it.

The following code assumes that the data to be exported

$content  = "Dhara,Senjaliya";
$content .= "\n";
$content .= "Jainish,Senjaliya";
$content .= "\n\n";
$content .= "Successfully download";		

$filename = 'CSV_FILE_NAME.csv';

$mimeType = 'application/octet-stream';
header('Content-Type: application/vnd.ms-excel');
header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Content-Disposition: attachment; filename="' . $filename . '"');

if (strstr($_SERVER['HTTP_USER_AGENT'],'MSIE'))
{
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
} else {
    header('Pragma: no-cache');
}

echo "$content"; 
die;

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya

 


 

Advertisements

How to download image, PDF, Excel in php


 
How to download image using in php,
How to download PDF using in php,
How to download Excel using in php,
How to download CSV using in php
How to download PSD using in php

For download images, pdf, excel, zip, csv, tiff, bmp, PSD, ico using php then you can use below code.

$inPath = "IMAGE_ROOT_PATH";
$outPath = "IMAGE_NAME";

$fullPath = $inPath.$outPath;
 
	$tmp = explode(".",$outPath);
	$fsize = filesize($fullPath);

	switch ($tmp[count($tmp)-1]) {
	
	  case "pdf": $ctype="application/pdf"; break;
	  case "exe": $ctype="application/octet-stream"; break;
	  case "zip": $ctype="application/zip"; break;
	  case "docx":
	  case "doc": $ctype="application/msword"; break;
	  case "csv":
	  case "xls":
	  case "xlsx": $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;
	  case "tif":
	  case "tiff": $ctype="image/tiff"; break;
	  case "psd": $ctype="image/psd"; break;
	  case "bmp": $ctype="image/bmp"; break;
	  case "ico": $ctype="image/vnd.microsoft.icon"; break;
	  default: $ctype="application/force-download";
	}
	
	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");
	header("Content-Disposition: attachment; filename=\"".$outPath."\";" );
	header("Content-Transfer-Encoding: binary");
	header("Content-Length: ".$fsize);
	ob_clean();
	flush();
	readfile($fullPath);
	die;

If you have any queries, please do not hesitate to contact me at Jainish Senjaliya