PHP 源码:PHP 超大文件下载,断点续传下载的方法
本文为大家整理了 PHP 超大文件下载,断点续传下载的解决方法,供大家参考下
最近导出的时候出现一个 php 内存溢出的问题,原因就是在于下载的时候读取生成的临
时文件过大,PHP 内存无法容纳,一开如是想到更改 PHP 内存限制,但是这个只是一个
缓兵之计,于是想到了另外一个方法是把文件分次读取,并下载。
以下是源代码:
<?php
$sourceFile
= "1.tmp";
//
要下载的临时文件名
$outFile
= "用户订单.xls";
//
下载保存到客户端的文件名
$file_extension
=
strtolower
(
substr
(
strrchr
(
$sourceFile
, "."), 1));
//
获取文件扩展名
//echo $sourceFile;
if
(!
ereg
("[tmp|txt|rar|pdf|doc]",
$file_extension
))
exit
("非法资源下载");
//
检测文件是否存在
if
(!
is_file
(
$sourceFile
)) {
die
("<b>404 File not found!</b>");
}
$len
=
filesize
(
$sourceFile
);
//
获取文件大小
$filename
=
basename
(
$sourceFile
);
//
获取文件名字
$outFile_extension
=
strtolower
(
substr
(
strrchr
(
$outFile
, "."), 1));
//
获取文件扩展名
//
根据扩展名 指出输出浏览器格式
switch
(
$outFile_extension
) {
case
"exe" :
$ctype
= "application/octet-stream";
break
;
case
"zip" :
$ctype
= "application/zip";
break
;
case
"mp3" :
$ctype
= "audio/mpeg";
break
;
case
"mpg" :
$ctype
= "video/mpeg";
break
;
case
"avi" :
$ctype
= "video/x-msvideo";
break
;
default
:
$ctype
= "application/force-download";