PHP 下载功能代码不兼容360浏览器,但火狐可以
如题,火狐正常,360提示未知大小,下载失败。
核心代码如下,大部分都是网上乱摘乱拿的。大神们看看什么问题导致
$fileName = $name ? $name : pathinfo($file,PATHINFO_FILENAME);
$filePath = realpath($file);
$fp = fopen($filePath,'rb');
if(!$filePath || !$fp){
header('HTTP/1.1 404 Not Found');
echo ' <script type="text/javascript">alert("找不到文件");</script>';
exit;
}
$fileName = $fileName .'.'. pathinfo($filePath,PATHINFO_EXTENSION);
$encoded_filename = urlencode($fileName);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
set_time_limit(0);
ini_get("memory_limit","512M");
header('HTTP/1.1 200 OK');
header("Content-type: application/octet-stream");
header("accept-Ranges: bytes");
header("Content-Length: ".filesize($filePath));
/* header("Accept-Length: ".filesize($filePath)); */
header( "Pragma: public" );
header( "Expires: 0" );
header('Content-Transfer-Encoding: binary');
$ua = $_SERVER["HTTP_USER_AGENT"];
if (preg_match("/MSIE/", $ua)) {
header('Content-Disposition: attachment; filename="' . $encoded_filename . '"');
} else if (preg_match("/Firefox/", $ua)) {
header('Content-Disposition: attachment; filename*="utf8\'\'' . $fileName . '"');
} else {
header('Content-Disposition: attachment; filename="' . $fileName . '"');
}
fpassthru($fp);
fclose ( $file );