File reading/downloading using php.

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));
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).”;”);


About amitsonikhandwa
I am a web developer working in PHP and MYSQL with AJAX, jQuery and JavaScript. I am in web development since 2007.

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s