将所有文件压缩为 zip 的 PHP 代码无法正常工作

Posted

技术标签:

【中文标题】将所有文件压缩为 zip 的 PHP 代码无法正常工作【英文标题】:Php code to compress all files as zip not working fine 【发布时间】:2016-06-15 13:05:32 【问题描述】:

我有这个脚本可以从数据库中以 zip 格式下载所有文件,但它只获取保存在数据库中的第一个文件 - 其他文件没有显示。

以下是我在数据库中的文件列表,我想将其包含在 zip 中:

lginin

logersutil.php

lgininh.js

Readme.md

还有我的代码:

<?php
 if(isset($_POST['download']))
try
$db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
$db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa");
$post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__)));
$post_stmt->execute();
while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC))

    $files = array(''.$rowss["jailchillink"].'','codejail.cj');

    # create new zip opbject
    $zip = new ZipArchive();

    # create a temp file & open it
    $tmp_file = tempnam('.','');
    $zip->open($tmp_file, ZipArchive::CREATE);

    # loop through each file
    foreach($files as $file)

        # download file
        $download_file = file_get_contents($file);

        #add it to the zip
        $zip->addFromString(basename($file),$download_file);

    

    # close zip
    $zip->close();

    # send the file to the browser as a download
    header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip');
    header('Content-type: application/zip');
    readfile($tmp_file);

 catch (PDOException $e) echo 'Connection failed: ' . $e->getMessage(); 
 
?>

【问题讨论】:

【参考方案1】:

在您当前的代码中,您基本上是在制作 1 个新的 zip 文件并为每一行返回它。由于客户端只能读取 1 个响应,因此他们显然不会看到超过 1 行。

因此,您需要做的是确保在发送之前将所有文件添加到 zip。像这样:

<?php
if(isset($_POST['download']))
    try
        $db_conn = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME,DB_USERNAME,DB_PASSWORD);
        $db_conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $post_stmt = $db_conn->prepare("SELECT * FROM jailorgchild WHERE jailchildbasname = :BasNameJa");
        $post_stmt->bindParam(':BasNameJa', basename(dirname(__FILE__)));
        $post_stmt->execute();

        // here we create a temporary array to hold all the filenames
        // and fill it with the file you always want to have inserted
        $files = array('codejail.cj');
        while ($rowss = $post_stmt->fetch(PDO::FETCH_ASSOC))
            // now fill the array with your files
            $files[] = $rowss['jailchillink'];
                   

        // and now we finally start processing them all, after having finished reading from the DB
        # create new zip opbject
        $zip = new ZipArchive();

        # create a temp file & open it
        $tmp_file = tempnam('.','');
        $zip->open($tmp_file, ZipArchive::CREATE);

        # loop through each file
        foreach($files as $file)
            # download file
            $download_file = file_get_contents($file);

            #add it to the zip
            $zip->addFromString(basename($file),$download_file);
        

        # close zip
        $zip->close();

        # send the file to the browser as a download
        header('Content-disposition: attachment; filename='.basename(dirname(__FILE__)).'.zip');
        header('Content-type: application/zip');
        readfile($tmp_file);

     catch (PDOException $e) 
        echo 'Connection failed: ' . $e->getMessage();
    

?>

【讨论】:

我收到此错误消息Warning: PDO::__construct(): php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\www.codejail.com\cjl\Code_editor\index.php on line 375 Connection failed: SQLSTATE[HY000] [2002] php_network_getaddresses: getaddrinfo failed: No such host is known. 感谢@Tularis,您的回答帮助了我,但您从数据库中获取文件时出错 抱歉,db-host 部分的小错字;那是假设您的数据库连接代码是准确的。你建议的编辑正是我想告诉你的,你应该做。您需要从 database-fetch 部分中删除压缩部分;首先获取所有信息,然后对其进行处理。

以上是关于将所有文件压缩为 zip 的 PHP 代码无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章

java解压zip文件至指定文件夹

压缩文件中不允许使用550个可执行文件

zip压缩包怎么解压

php打包文件为ZIP包后下载到本地

压缩目录中的所有文件并下载生成的.zip

Linux zip命令的分卷压缩和合并解压