您可以使用 URL 重写将文件名作为参数传递给脚本。您的脚本(本例中为 downloadexecutable.php)将响应包含“下载”的 $_GET 参数“q”:
<?php
if (isset($_GET['q']) && $_GET['q'] == "download") {
//you will want to:
//1) set Content-type to be the correct type...I just set it to octet-stream because I'm not sure what it should be
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename('http://code.google.com/fold/file1.exe').'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize('http://code.google.com/fold/file1.exe')); // provide file size
echo file_get_contents('http://code.google.com/fold/file1.exe'); // push it out
exit()
}
?>
然后,您将在根目录中的 .htaccess 文件中启用 Apache 上的 URL 重写,如下所示:
RewriteEngine On
RewriteRule ^(.+)\.exe$ /downloadexecutable.php?q=$1 [NC,L]
当用户请求任何以 .exe 结尾的文件时,downloadexecutable.php 应该执行如下:
请求:http: //yoursite.com/download.exe
PHP实际处理了什么:http: //yoursite.com/downloadexecutable.php ?q=download
请求:http: //yoursite.com/this/could/be/a/bug.exe
处理:http: //yoursite.com/downloadexecutable.php ?q=this/could/be/a/bug
显然它需要一些工作,我还没有测试过上述任何一个,但如果你稍微玩弄一下并且愿意使用谷歌,你应该能够让它工作。
URL重写教程: http: //www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
php东西的来源: http: //snipplr.com/view/22546/