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.

  1. $filename=”directory/filename”;//In $filename will have to provide full path of file.
  2. header(“Pragma: public”);
  3. header(“Expires: 0”);
  4. header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
  5. header(“Content-Type: application/force-download”);
  6. header(“Content-Type: application/octet-stream”);
  7. header(“Content-Type: application/download”);
  8. header(“Content-Disposition: attachment; filename=”.basename($filename).”;”);
  9. header(“Content-Transfer-Encoding: binary”);
  10. header(“Content-type: application/pdf”);
  11. header(“Content-Length: “.filesize($filename));
  12. readfile(“$filename”);
  13. 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).”;”);


eXTReMe Tracker