我编写了下面的PHP函数来下载文件,它按预期工作。 然而,当我尝试下载此文件:curl_exec成功,但输出文件为空
$url = 'http://javadl.sun.com/webapps/download/GetFile/1.7.0_02-b13/windows-i586/jre-7u2-windows-i586-iftw.exe';
download($url);
...没有内容被写入文件。我无法弄清楚为什么。该文件已创建,对curl_exec的调用返回true,但输出文件保持为空。该文件可以在浏览器中下载,并且该功能可以成功下载其他文件。只是这个文件(主机?),我有问题。
任何帮助表示赞赏。
function download($url)
{
$outdir = 'C:/web/www/download/';
// open file for writing in binary mode
if (!file_exists($outdir)) {
if (!mkdir($outdir)) {
echo "Could not create download directory: $outdir.\n";
return false;
}
}
$outfile = $outdir . basename($url);
$fp = fopen($outfile, 'wb');
if ($fp == false) {
echo "Could not open file: $outfile.\n";
return false;
}
// create a new cURL resource
$ch = curl_init();
// The URL to fetch
curl_setopt($ch, CURLOPT_URL, $url);
// The file that the transfer should be written to
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, false);
$header = array(
'Connection: keep-alive',
'User-Agent: Mozilla/5.0',
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// downloading...
$downloaded = curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
fclose($fp);
if (!$downloaded) {
echo "Download failed: $error\n";
return false;
} else {
echo "File successfully downloaded\n";
return $outfile;
}
}
如果您尝试vardump的$下载变量和/或删除CURLOPT_FILE,你看到什么了未来通过对卷曲通常会发生什么? – DaOgre 2011-12-21 01:06:45