Home > Backend Development > PHP Tutorial > Examples of two methods for packaging and downloading multiple files in PHP

Examples of two methods for packaging and downloading multiple files in PHP

零下一度
Release: 2023-03-14 08:08:01
Original
3031 people have browsed it

I recently sorted out the documents and found an example code for php multi-file packaging and downloading. I sorted it out a little and streamlined it to share php multi-file compression and downloading.

When multiple files need to be downloaded at the same time, most browsers do not support simultaneous downloading of multiple files. You can use JavaScript scripts to dynamically generate multiple links, but this requires the user multiple times. Click the download dialog box, the user experience is not good, and some browsers are not compatible. At this time, multiple file package downloads can help you solve this problem.

/*php multi-file compression and download*/

function addFileToZip($path,$zip){
        $handler=opendir($path); //打开当前文件夹由$path指定。
        while(($filename=readdir($handler))!==false){
            if($filename != "." && $filename != ".."){//文件夹文件名字为'.'和‘..’,不要对他们进行操作
                if(is_dir($path."/".$filename)){// 如果读取的某个对象是文件夹,则递归
                    addFileToZip($path."/".$filename, $zip);
                }else{
                    //将文件加入zip对象;并减少目录层
                    $zip->addFile($path.$filename, $filename);
                }
            }
        }
        @closedir($path);
    }
     
    //要下载的文件夹路径
    $filePath = '..'.$_GET['activepath']. '/'. $_GET['filename'].'/';
    //生成压缩文件名字
    $zipFileName = '../download/'.$_GET['filename'] .'.zip';
 
    $zip=new ZipArchive();
    if($zip->open($zipFileName, ZipArchive::OVERWRITE)=== TRUE){
        addFileToZip($filePath, $zip); //调用方法,对要打包的根目录进行操作,并将ZipArchive的对象传递给方法
        $zip->close(); //关闭处理的zip文件
    }
     
    if(!file_exists($zipFileName))
    {
        echo '文件压缩失败!或者未生成压缩包!!';
        exit;
    }
     
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header('Content-disposition: attachment; filename='.basename($zipFileName)); //文件名
    header("Content-Type: application/zip"); //zip格式的
    header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
    header('Content-Length: '. filesize($zipFileName)); //告诉浏览器,文件大小
    @readfile($zipFileName);
Copy after login
$filename = "test.zip";
$datalist=array('./pubfile/1.jpg','./pubfile/2.jpg');
if(!file_exists($filename)){
 $zip = new ZipArchive();
 if ($zip->open($filename, ZipArchive::CREATE)==TRUE) {
  foreach( $datalist as $val){
   if(file_exists($val)){
    $zip->addFile( $val, basename($val));
   }
  }
  $zip->close();
 }
}
if(!file_exists($filename)){
 exit("无法找到文件");
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($filename)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小
@readfile($filename);
Copy after login

The above is the detailed content of Examples of two methods for packaging and downloading multiple files in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template