Advertisement
Chrizz93

Untitled

Apr 21st, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.26 KB | None | 0 0
  1. <?php
  2. // block any attempt to the filesystem
  3. if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
  4.     $filename = $_GET['file'];
  5. } else {
  6.     $filename = NULL;
  7. }
  8. // define error message
  9. $err = 'Sorry, the file you are requesting is unavailable.';
  10. ?>
  11.  
  12. <?php
  13.  
  14. if (!$filename) {
  15. // if variable $filename is NULL or false display the message
  16.     echo $err;
  17. } else {
  18. // define the path to your download folder plus assign the file name
  19.     $path = 'downloads/' . $filename;
  20. // check that file exists and is readable
  21.     if (file_exists($path) && is_readable($path)) {
  22. // get the file size and send the http headers
  23.         $size = filesize($path);
  24.         header('Content-Type: application/octet-stream');
  25.         header('Content-Length: ' . $size);
  26.         header('Content-Disposition: attachment; filename=' . $filename);
  27.         header('Content-Transfer-Encoding: binary');
  28. // open the file in binary read-only mode
  29. // display the error messages if the file can´t be opened
  30.         $file = @ fopen($path, 'rb');
  31.         if ($file) {
  32. // stream the file and exit the script when complete
  33.             fpassthru($file);
  34.             exit;
  35.         } else {
  36.             echo $err;
  37.         }
  38.     } else {
  39.         echo $err;
  40.     }
  41. }
  42. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement