有人可以建议为什么不下载PDF吗?当我点击下载时,总是显示为网络错误。 PDF文件的大小为95 MB,这是网络错误的原因吗?我正在使用file_get_contents()
浏览文件。
答案 0 :(得分:0)
您可以尝试使用cURL:
// downloads a file and stores it into a file
function getPage($url,$file_ptr)
{
$ch = curl_init();
curl_setopt_array($ch, Array(
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36",
CURLOPT_SSL_VERIFYPEER => FALSE,
CURLOPT_HEADER => FALSE,
CURLOPT_AUTOREFERER => TRUE,
CURLOPT_FOLLOWLOCATION => TRUE,
CURLOPT_MAXREDIRS => 5,
CURLOPT_URL => $url,
CURLOPT_REFERER => $url,
CURLOPT_CONNECTTIMEOUT => 10,
CURLOPT_FILE => $file_ptr
));
$result = curl_exec($ch);
if(curl_errno ($ch)) $err = curl_error($ch);
else $err = '';
curl_close($ch);
if($err != '') return $err;
return $result;
}
$in_file = tempnam(sys_get_temp_dir(), 'pdf_');
$fp = fopen ($in_file, 'w+');
$ret = getPage($url,$fp);
if($ret === TRUE)
{
fclose($fp);
// do something with the file
}
else
{
fclose($fp);
@unlink($in_file);
}