shosei

Universal force file download

Mar 11th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.98 KB | None | 0 0
  1. // URL: http://snipplr.com/
  2. // URL: http://www.mechanicmatt.com/
  3.  
  4. // so lets say you saved this script in your document root as "dlFile.php". so it's located at yourdomain.com/dlFile.php
  5. //
  6. // and your files are located at yourdomain.com/files/
  7. //
  8. // then you would link to it like this
  9. //
  10. // href="dlFile.php?filename=podcast1.mp3". If your file is in a subdirectory of /files/ then you'd use
  11. //
  12. // dlFile.php?filename=subDir/filename.jpg
  13. //
  14. // Refer to the comment in the php script to specify the path where the files are stored.
  15.  
  16.  
  17. <?php
  18. if ($_REQUEST["filename"] != "") {
  19.         $filename = str_replace("../", "", $_REQUEST["filename"]);
  20.         if ($filename != "") {
  21.                
  22.                 /*
  23.                         this is where you set the base path and the file for security reasons.
  24.                         If you wish to manually hard code a base path, then comment out the following line,
  25.                         and use the line after it
  26.                 */
  27.                 $file = dirname(__FILE__) . "/files/" . $filename;
  28.                
  29.                 //$file = "/var/www/htdocs/audio/" . $filename;
  30.                
  31.                
  32.                 if (file_exists($file)) {
  33.                         header ("Content-type: octet/stream");
  34.                         header ("Content-disposition: attachment; filename=" . str_replace(" ", "_", basename($file)) . ";");
  35.                         header ("Content-Length: " . filesize($file));
  36.                         readfile($file);
  37.                         die();
  38.                 }
  39.                 else {
  40.                         echo '
  41.                                <script type="text/javascript">
  42.                                <!--
  43.                                alert("' . $file . '\n\nI do not currently have this image ready for download");
  44.                                -->
  45.                                </script>
  46.                        ';
  47.                 }
  48.         } else {}
  49. } else {}
  50. ?>
Add Comment
Please, Sign In to add comment