.zip文件下载为.php
我有一个使用php在服务器上生成的.zip文件。生成的文件是有效的,我通过ftp等下载文件进行了检查 我需要为用户创建一种方法,在生成该文件后下载该文件,然后删除该文件。这是我要发送的标题.zip文件下载为.php,php,javascript,zipfile,Php,Javascript,Zipfile,我有一个使用php在服务器上生成的.zip文件。生成的文件是有效的,我通过ftp等下载文件进行了检查 我需要为用户创建一种方法,在生成该文件后下载该文件,然后删除该文件。这是我要发送的标题 header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-control: public"); heade
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-control: public");
header("Content-Description: File Transfer");
header('Content-type: application/zip');
header("Content-Transfer-Encoding: Binary");
header('Content-Disposition: attachment; filename="'.basename($archive_file_name).'"');
header("Content-Length: " . filesize($this->dirPath."/".$archive_file_name) );
ob_clean();
//echo is_file($this->dirPath."/".$archive_file_name);
readfile($this->dirPath."/".$archive_file_name);
unlink($this->dirPath."/".$archive_file_name);
exit;
当我尝试下载前几次时,上面的代码可以工作,但几次之后,它开始以.php文件而不是.zip文件的形式下载
通过转到启动zip文件创建的特定链接触发文件下载。完成后,它会发送标题以开始下载尝试此标题,您可以使用resume下载此标题 我建议使用数据库或日志文件来理解下载的文件,因为这个头文件可以下载部分 从下载程序获取请求范围您可以使用fseek读取文件的范围
$Range=@$_SERVER['HTTP_RANGE'];//Range: bytes=843530240-
$Cach="";
if($Range!=""){
$Range=explode("=",$Range);
$Cach=(int)trim($Range[1]," -");
}
缓存控制器
function caching_include($file) {
$headers = $_SERVER;
// Get the modification timestamp
if(!(list(,,,,,,,,,$lastModified) = @stat($file))){
echo Error;
exit();
}
// Build our entity tag
$eTag = "ci-".dechex(crc32($file.$lastModified));
if (false and (strpos(@$headers['If-None-Match'], "$eTag")) &&(gmstrftime("%a, %d %b %Y %T %Z", $lastModified) == @$headers['If-Modified-Since'])) {
// They already have an up to date copy so tell them
header('HTTP/1.1 304 Not Modified');
header('Cache-Control: private');
header("Pragma: ");
header('Expires: ');
header('Content-Type: ');
header('ETag: "'.$eTag.'"');
exit;
} else {
// We have to send them the whole page
header('Cache-Control: private');
header('Pragma: ');
header('Expires: ');
header('Last-Modified: '.gmstrftime("%a, %d %b %Y %T %Z",$lastModified));
header('ETag: "'.$eTag.'"');
}
}
下载头
header("Server: SepidarSoft/ir-system");//server name
header("Date: ".date("a,d b Y H:M:S GMT",time()));
header("X-Powered-By: SepidarSoft");
header("Cache-Control: public");
header("Content-disposition:filename=\"".$BaseName."\"");//file name
caching_include($FPatch);//caching controller
header("Accept-Ranges:bytes");
header("Content-Transfer-Encoding: binary\n");
header("Connection: Keep-Alive");
header("Content-Type: $Type");//file type like application/zip
header("Content-Length: ".($FSize-$Cach)); //length of download it is for resume
header("Content-Range: bytes ".$Cach."-".($FSize-1)."/".$FSize); //download range it is for resume
header("HTTP/1.1 206 Partial Content",true);
set_time_limit(0);
如果使用
设置时间限制(0)代码>在代码中,如果用户连接变为断开连接,请继续,直到完成工作,因为您的文件将被删除。在一次请求后,您可以使用连接\u status()==0
检查连接,如下所示
$Speed=102400;
fseek($Handle,$From);//if use resume it is useful
while(!@feof($Handle) and (connection_status()==0)){
print(fread($Handle,$Speed));
flush();
ob_flush();
}
if(connection_status()==0){//with this condition you can understand download is down by user
unlink($this->dirPath."/".$archive_file_name);
}
你的代码看起来很正确。但是,您需要确保代码前面和后面的空格。
在代码中添加标题时,空间将中断,无法加载zip文件。
再次检查并删除页面顶部和底部的空格
还有一行写着
readfile($this->dirPath.“/”$archive\u file\u name)
可以从代码中删除试试这个“上面的代码在我尝试下载前几次时有效”多少次??我下载了超过15次,效果很好。你需要多少钱?如果用户连接断开,请继续,直到完成此工作,你的文件将被删除。在一个请求后,你可以使用connection\u status()==0
检查连接,如下图所示。有关更多信息,请参阅答案。另请参阅:是的。。空间是罪魁祸首。。正在使用多个文本编辑器编辑我的文件。。我想在下面的某个地方有额外的空间。在这上面浪费了很多时间!