Download.php

出自ProgWiki
跳至導覽 跳至搜尋

用途

程式碼

<?php
// 網站名稱 (避免盜連使用)
define( "MyWebHost", "www.player.idv.tw" );
 
// 下載檔案的預設位置 (基於安全性, 避免網站上的其他路徑的檔案被下載)
define( "PathDownload", "/home/player/DownloadFiles/" );
 
$url_REFERER = @parse_url($_SERVER['HTTP_REFERER']);
 
// 防止盜連
if ($url_REFERER['host'] != MyWebHost) {
	die();
}
 
// 沒有傳入檔名與路徑
if (!isset($_GET['file'])) {
	die();
}
// Website url to open
$file = PathDownload . $_GET['file']; // or $REQUEST would suffice?
 
if (file_exists($file)) {
	header("Content-Type: application/octet-stream");
	header("Content-Transfer-Encoding: Binary"); 
	header("Content-Disposition: attachment; filename=\"" . basename($file) . "\""); 
	header("Expires: 0");
	header("Cache-Control: must-revalidate");
	header("Pragma: public");
 
	readfile($file);
} else {
    // 檔案不存在
    header("ContentType: text/plain");
    echo "The file $file does not exist";
}
?>