【发布时间】:2015-06-10 21:14:50
【问题描述】:
我编写了一个使用 curl 扩展名下载一些 (.exe) 文件的 php 函数。该文件已成功下载,但是当我尝试打开它时出现不兼容错误。我使用记事本++打开它,我看到文件开头添加了一个“200”。我真的不明白这个“200”是从哪里来的? 这是我的功能:
$source = isset($_GET['link']) ? $_GET['link'] : ''; #get the download link
$filename = isset($_GET['name']) ? $_GET['name'] : 'download.exe'; # define name
if($source != '')
{
$handle = curl_init($source);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE);
/* Get the HTML or whatever is linked in $url. */
$response = curl_exec($handle);
/* Check for 403 (forbidden). */
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE);
if($httpCode == 403) {
echo "<h2> <font color='red'> Sorry you are not allowed to download that file.</font><h2>";
} else {
header("Content-Disposition: attachment; filename=\"{$filename}\"");
#header("Content-Disposition: attachment; filename=\"uploaded.pdf\"");
// Get a FILE url to my test document
$url= str_replace(" ","%20", $source);
$ch= curl_init($url);
#curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_exec($ch);
curl_close ($ch);
}
curl_close($handle);
}
else {
echo "error";
}
【问题讨论】:
-
200 是http响应码表示OK。
-
是的,我知道,但为什么它写在文件里面?