File Download Script

This sections will contains code regarding different PHP & MySQL features and contains help about php/mysql issues.

File Download Script

Postby MT Shahzad on March 20th, 2008, 9:51 am

The following php code will allow you to make any file on your server downloadable like pdf, doc, csv, etc files which are normally open in the browser by default.

You can also use this script if you want to hide the path of files on your server from the users.

Code: Select all
<?
   @ini_set("max_execution_time", 0);

       // File Name with complete path
       // $sFile = "";

   $fFileSize = @filesize($sFile);

   if(ini_get('zlib.output_compression'))
      @ini_set('zlib.output_compression', 'Off');

   header('Content-Description: File Transfer');
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: private", false);
   header('Content-Type: application/force-download');
   header("Content-Type: application/download");
   header("Content-Type: text/csv");
   header("Content-Disposition: attachment; filename=\"".@basename($sFile)."\";");
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: $fFileSize");

   @readfile($sFile);
?>
MT Shahzad
Web/Software Developer
http://mts.sw3solutions.com
User avatar
MT Shahzad
Site Admin
 
Posts: 300
Joined: February 29th, 2008, 8:11 am
Location: Muridke, Pakistan

Return to PHP / MySQL / XML

Who is online

Users browsing this forum: No registered users and 0 guests