繁体   English   中英

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

[英]Php code to compress all files as zip not working fine

我有此脚本可以从数据库中以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();} 
 }
?>

在当前代码中,您基本上是在制作一个新的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();
    }

}?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM