Quantcast
Channel: Dallas Clark - Web Developer, Facebook Developer, iPhone Developer, iPad Developer » script
Viewing all articles
Browse latest Browse all 12

Hide the paths to files on your server with PHP

$
0
0

If you want users to download files off your site but you don't want to expose the links of the files on the server, the script below in PHP is an excellent way to hide your files to your viewers.

Basically the script works by changing your browser's mime type to application/octet-stream, you can then pass the file through this page ($file in the script below), and name the file however you wish the end user to view it as ($filename in the script below).

When the user is downloading the file they see the source of the download as the page rather then the actual file ie: downloadFile.php rather then downloadFile.zip.

  1. < ?php
  2. $mm_type="application/octet-stream";
  3. $file = "downloadFile.zip";
  4. $filename = "download.zip";
  5.  
  6. header("Cache-Control: public, must-revalidate");
  7. header("Pragma: no-cache");
  8. header("Content-Type: " . $mm_type);
  9. header("Content-Length: " .(string)(filesize($file)) );
  10. header('Content-Disposition: attachment;
  11. filename="'.$filename.'"');
  12. header("Content-Transfer-Encoding: binary\n");
  13.  
  14. readfile($file);
  15. ?>

Click to see this script in action.

You can add security to this script to only allow authenticated users and more. If you have any queries or suggestions for this script, please don't hesitate to comment below.


Viewing all articles
Browse latest Browse all 12

Trending Articles