0

我有一个托管在 code.google.com 的可执行文件(比如http://code.google.com/fold/file1.exe)。当用户浏览到http://mysite.com/download.exe时,我希望他们自动获取文件http://code.google.com/fold/file1.exe的内容,而无需任何重定向。用户应该认为他/她正在从http://mysite.com/download.exe而不是http://code.google.com/fold/file1.exe下载文件。

我怎么能做到这一点,使用 PHP?这个过程有什么特殊的说法吗?

4

2 回答 2

2

您可以使用 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/

于 2012-09-02T06:16:42.077 回答
1

您可能想尝试这种方法:

<?
function get_file($url) {
$ficheiro = "file.exe";
$fp = fopen (dirname(__FILE__) . "/$ficheiro", 'w+'); //make sure you've write permissions on this dir
//curl magic
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
fclose($fp);
}

//sets the original file url
$url = "http://code.google.com/fold/file1.exe";

//downloads original file to local dir
get_file($url);

//redirect user to the download file hosted on your site
header("Location: $ficheiro");
?>

并将其添加到您的 .htaccess 中:

RewriteEngine On
RewriteRule ^download.exe  this_script.php
于 2012-09-02T06:17:26.857 回答