使用 php 将多个文件下载为 zip 文件

发布于 2024-08-11 07:57:35 字数 32 浏览 3 评论 0原文

如何使用 php 将多个文件下载为 zip 文件?

How can I download multiple files as a zip-file using php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一百个冬季 2024-08-18 07:57:35

您可以使用 ZipArchive 类来创建ZIP 文件并将其流式传输到客户端。类似于:

$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

并流式传输:

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

第二行强制浏览器向用户显示一个下载框并提示名称 filename.zip。第三行是可选的,但某些(主要是较旧的)浏览器在某些情况下会出现问题,而未指定内容大小。

You can use the ZipArchive class to create a ZIP file and stream it to the client. Something like:

$files = array('readme.txt', 'test.html', 'image.gif');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
  $zip->addFile($file);
}
$zip->close();

and to stream it:

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);

The second line forces the browser to present a download box to the user and prompts the name filename.zip. The third line is optional but certain (mainly older) browsers have issues in certain cases without the content size being specified.

离旧人 2024-08-18 07:57:35

这是用 PHP 制作 ZIP 的工作示例:

$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name,  ZipArchive::CREATE);
foreach ($files as $file) {
  echo $path = "uploadpdf/".$file;
  if(file_exists($path)){
  $zip->addFromString(basename($path),  file_get_contents($path));  
  }
  else{
   echo"file does not exist";
  }
}
$zip->close();

This is a working example of making ZIPs in PHP:

$zip = new ZipArchive();
$zip_name = time().".zip"; // Zip name
$zip->open($zip_name,  ZipArchive::CREATE);
foreach ($files as $file) {
  echo $path = "uploadpdf/".$file;
  if(file_exists($path)){
  $zip->addFromString(basename($path),  file_get_contents($path));  
  }
  else{
   echo"file does not exist";
  }
}
$zip->close();
红ご颜醉 2024-08-18 07:57:35

创建一个zip文件,然后下载该文件,通过设置标头,读取zip内容并输出文件。

http://www.php.net/manual/en/function。 ziparchive-addfile.php

http://php.net/manual/en /function.header.php

Create a zip file, then download the file, by setting the header, read the zip contents and output the file.

http://www.php.net/manual/en/function.ziparchive-addfile.php

http://php.net/manual/en/function.header.php

南笙 2024-08-18 07:57:35

你已经准备好使用 php zip lib 了,
也可以使用 zend zip lib,

<?PHP
// create object
$zip = new ZipArchive();   

// open archive 
if ($zip->open('app-0.09.zip') !== TRUE) {
    die ("Could not open archive");
}

// get number of files in archive
$numFiles = $zip->numFiles;

// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
    $file = $zip->statIndex($x);
    printf("%s (%d bytes)", $file['name'], $file['size']);
    print "
";    
}

// close archive
$zip->close();
?>

http:// devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/

并且还有 php pear lib 用于此
http://www.php.net/manual/en/class.ziparchive.php

You are ready to do with php zip lib,
and can use zend zip lib too,

<?PHP
// create object
$zip = new ZipArchive();   

// open archive 
if ($zip->open('app-0.09.zip') !== TRUE) {
    die ("Could not open archive");
}

// get number of files in archive
$numFiles = $zip->numFiles;

// iterate over file list
// print details of each file
for ($x=0; $x<$numFiles; $x++) {
    $file = $zip->statIndex($x);
    printf("%s (%d bytes)", $file['name'], $file['size']);
    print "
";    
}

// close archive
$zip->close();
?>

http://devzone.zend.com/985/dynamically-creating-compressed-zip-archives-with-php/

and there is also php pear lib for this
http://www.php.net/manual/en/class.ziparchive.php

紅太極 2024-08-18 07:57:35

我打开该 zip 文件时会出现此弹出窗口
加载存档时发生错误。
给出解决方案
我的代码

if (isset($_POST['selectedFiles'])) {
            // Handle form submission
            $selectedFiles = $_POST['selectedFiles'];
            $zip = new ZipArchive();
            $error = "";
            $zipname = 'selected_files.zip';
            $zip->open($zipname, ZipArchive::CREATE);
            foreach ($selectedFiles as $fileName) {
                $filePath = realpath('..').'/QUERY_FILE/'. $fileName;
                if (file_exists($filePath)) {
                    $added = $zip->addFile($filePath, $fileName);
                    if (!$added) {
                        $error .= "* Failed to add file to ZIP: $fileName\n";
                    }
                } else {
                    $error .= "* File not found: $fileName\n";
                }
            }
            $zip->close();
            if (file_exists($zipname)) {
                // Download the zip file
                header('Content-Type: application/zip');
                header('Content-disposition: attachment; filename='.$zipname);
                header('Content-Length: ' . filesize($zipname));
                readfile($zipname);
                // Delete the zip file after download
                // unlink($zipname);
            }   
            if (!empty($error)) {
                echo "Errors occurred while creating the ZIP file:<br>" . $error;
            }
        } else {
            echo 'No files selected';
        }

I open that zip file occur this popup
An error occurred while loading the archive.
give solution
MY code

if (isset($_POST['selectedFiles'])) {
            // Handle form submission
            $selectedFiles = $_POST['selectedFiles'];
            $zip = new ZipArchive();
            $error = "";
            $zipname = 'selected_files.zip';
            $zip->open($zipname, ZipArchive::CREATE);
            foreach ($selectedFiles as $fileName) {
                $filePath = realpath('..').'/QUERY_FILE/'. $fileName;
                if (file_exists($filePath)) {
                    $added = $zip->addFile($filePath, $fileName);
                    if (!$added) {
                        $error .= "* Failed to add file to ZIP: $fileName\n";
                    }
                } else {
                    $error .= "* File not found: $fileName\n";
                }
            }
            $zip->close();
            if (file_exists($zipname)) {
                // Download the zip file
                header('Content-Type: application/zip');
                header('Content-disposition: attachment; filename='.$zipname);
                header('Content-Length: ' . filesize($zipname));
                readfile($zipname);
                // Delete the zip file after download
                // unlink($zipname);
            }   
            if (!empty($error)) {
                echo "Errors occurred while creating the ZIP file:<br>" . $error;
            }
        } else {
            echo 'No files selected';
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文