Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $filename = $_GET['file'];
- // NOTE: YOU SHOULD HARDCODE IN A FILE LOCATION OR RESTRICT DIRECTORIES SO THAT THE ACCESS IS RESTRICTED
- //FOR EXAMPLE
- //if(substr($filename, 0,5)!='mydir') { exit; } //restrict download access to a link beginning with mydir only
- //the link would then be something like http://mysite.com/force-download.php?file=mydir/myFile.jpg
- //http://elouai.com/force-download.php
- //EXAMPLE LINK http://mysite.com/force-download.php?file=bookmarks/apple-heart.jpg
- // required for IE, otherwise Content-disposition is ignored
- if(ini_get('zlib.output_compression'))
- ini_set('zlib.output_compression', 'Off');
- // addition by Jorg Weske
- $file_extension = strtolower(substr(strrchr($filename,"."),1));
- if( $filename == "" )
- {
- echo "<html><body OnLoad=\"javascript: alert('Sorry, file not found...');history.back();\" bgcolor=\"#F0F0F0\"></body></html>";
- exit;
- } elseif ( ! file_exists( $filename ) )
- {
- echo "<html><body OnLoad=\"javascript: alert('Sorry, file not found...');history.back();\" bgcolor=\"#F0F0F0\"></body></html>";
- exit;
- };
- switch( $file_extension )
- {
- case "pdf": $ctype="application/pdf"; break;
- case "exe": $ctype="application/octet-stream"; break;
- case "zip": $ctype="application/zip"; break;
- case "doc": $ctype="application/msword"; break;
- case "xls": $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;
- 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");
- // change, added quotes to allow spaces in filenames, by Rajkumar Singh
- header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );
- header("Content-Transfer-Encoding: binary");
- header("Content-Length: ".filesize($filename));
- readfile("$filename");
- exit();
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement