duangu1868
2015-01-28 11:16
浏览 55
已采纳

使用PHP从MySQL创建zip文件

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);


?>

P.S.: I have limited PHP knowledge.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dousi1970 2015-04-22 11:40
    已采纳

    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);
    
    }
    }
    
    点赞 评论
  • dongting3135 2015-01-28 11:32

    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)

    点赞 评论

相关推荐 更多相似问题