| PHP: Hiding a file's real path when downloading (prevent hotlinking) |
|
|
| Thursday, 11 October 2007 | |
|
Want to prevent people from linking to your downloads? This script will force a page to be loaded before the download starts. HTML header statements are used to trigger the download of the file. PHP is used to push the file to the browser. Principles HTML headers must be sent before any output is sent to the browser. PHP uses the header function to pass raw HTML headers. For this example we're going to get the filename from the URL www.yourdomain.com/download.php?file=download.zip. <?php $dir="/path/to/file/"; if (isset($_REQUEST["file"])) { $file=$dir.$_REQUEST["file"]; header("Content-type: application/force-download"); header("Content-Transfer-Encoding: Binary"); header("Content-length: ".filesize($file)); header("Content-disposition: attachment; filename=\"".basename($file)."\""); readfile("$file"); } else { echo "No file selected"; } ?>
|
| < Prev | Next > |
|---|