.zip文件下载为.php

时间:2013-04-09 05:07:58

标签: 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");
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文件的特定链接来触发文件下载。完成后,它会发送标题以开始下载

2 个答案:

答案 0 :(得分:1)

你的代码看起来很合适。但是,您需要确保代码的前后空格 在代码中添加标题时,空格会中断并且不会加载zip文件 再次检查并删除页面顶部和底部的空格。

一行说明 ReadFile的($这 - > dirPath “/” $ archive_file_name);
可以从代码中删除

答案 1 :(得分:0)

尝试此标题可供您下载简历 我建议使用数据库或日志文件来了解下载的文件与否,因为这个标题可用下载器下载部分

从下载程序获取请求范围,您可以使用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);

如果在代码中使用set_time_limit(0);,如果用户连接变为断开连续php继续直到完成工作,您的文件将被删除,您是否可以在一次请求后下载connection_status()==0进行检查连接,如下所示< / p>

$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);
 }