简体   繁体   中英

Php code to compress all files as zip not working fine

I have this script to download all files as zip from database, but it only gets the first file saved in database - the others are not showing.

Below is a list of the files I have in the database, which I want to include in the zip:

lginin

logersutil.php

lgininh.js

Readme.md

And my code:

<?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();} 
 }
?>

In your current code, you're basically making 1 new zip file and returning it for each row. Since the client can only read 1 response, they will obviously never see more than 1 row.

So what you need to do, is make sure to add all files to the zip before you send it out. Like so:

<?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();
    }

} ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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