File reading/downloading using php.

December 23, 2009 | Filed Under PHP | Comments Off

This is the example, how to download pdf file using php. you can also use this code for zip file,word file,image file, any type of media file.

$filename=”directory/filename”;//In $filename will have to  provide full path of file.

header(“Pragma: public”);

header(“Expires: 0″);

header(“Cache-Control: must-revalidate, post-check=0, pre-check=0″);

header(“Content-Type: application/force-download”);

header(“Content-Type: application/octet-stream”);

header(“Content-Type: application/download”);

header(“Content-Disposition: attachment; filename=”.basename($filename).”;”);

header(“Content-Transfer-Encoding: binary”);

header(“Content-type: application/pdf”);

header(“Content-Length: “.filesize($filename));

readfile(“$filename”);

exit();

If your file is pdf then use header(“Content-type: application/pdf”);

If zip file then use  header(“Content-type: application/zip”);

In $filename provide full path of file.

Ex. If your file is in  pdf folder and your php file is in  outside of pdf folder.

Then $filename=”pdf/filename.pdf”;//  will be.

If you want to display file not download, You have to remove below header  from above code.

header(“Content-Disposition: attachment; filename=”.basename($filename).”;”);


Comments

Comments are closed.

© PHPInterviewQuestion.com 2009 - 2012

eXTReMe Tracker