简体   繁体   中英

Create zip archive out of file from MySQL using PHP

I am trying to put a XML file from MySQL database to a zip archive before downloading. I am able to create zip file but Windows is giving error while opening it.

error msg: window cannot open the foler. filename.zip is invalid

Here is my code:

 <?php

if(isset($_GET['id'])) 
{

    $id  = intval($_GET['id']);


    if($id <= 0) {
        die('The id is invalid!');
    }
    else 
{
        // Connect to the database
        $dbLink = new mysqli('localhost', 'root', 'root', 'db');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }



 $zip = new ZipArchive();
$filename = "export_".date('Y.m.d H.i').".zip";

if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true)
{
    echo "Cannot Open for writing";
}


$zip->addEmptyDir('Section');

$query = "SELECT name,data FROM file_section WHERE id ='$id'";   
$result = $dbLink->query($query);


 if($result->num_rows == 1) 
     {          
 // Get the row

     $row = mysqli_fetch_array($result);
 }
while($row = mysql_fetch_array($result))
{
    $zip->addFile($row['data'], "{$row['name']}.xml");
}

$zip->close();



    header("Content-disposition: attachment; filename='.$filename'");
    header('Content-type: application/zip');


    readfile($fileName);
    }


            // Free the mysqli resources
            mysqli_free_result($result);
        }
        else {
            echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
        }
        mysqli_close($dbLink);


?>

PS: I have limited PHP knowledge.

Test this : create a temp file before addFile your zip object see http://php.net/manual/en/ziparchive.addfile.php

addFile method wait first parameter a filename (the path to the file to add)

Here is the working solution that I got later. Its valid for one single file id input.

<?php
// Make sure an id was passed
if (isset($_GET['ids'])) {
// Get the id into string (in case of an array)
$id_pass = implode(",",$_GET['ids']);
// Make sure the name is in fact a valid
if ($id_pass <= 0) {
    die ('No ID Selected!');
} else

 {
// Connect to the database
    $dbLink = new mysqli('localhost', 'root', 'password', 'DB Name');
    if (mysqli_connect_errno()) {
        die("MySQL connection failed: " . mysqli_connect_error());
    }

        // Fetch the file information
        $query = "select id, name, data from datatable  where id = {$id_pass};";
        $result = $dbLink->query($query);

        if ($result) {
            // Make sure the result is valid
            if ($result->num_rows == 1) {
                // Get the row
                $row = mysqli_fetch_assoc($result);

                //zip function

                $zip = new ZipArchive();
                $filename = "export_" . date('Y.m.d H.i.s') . ".zip";

                if ($zip->open($filename, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE) !== true) {
                    echo "Cannot Open for writing";
                }


                $ext = $row['name'] . ".xml"; // taking file name from DB and adding extension separately
                $zip->addFromString($ext, $row['data']); //adding blob data from DB
                $zip->close();

             header("Content-disposition: inline; filename='.$filename'");
            header('Content-type: application/zip');
            readfile($filename);
            unlink($filename);
            }
        }

    // Free the mysqli resources     
        mysqli_free_result($result);
         mysqli_close($dbLink);

}
}

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