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);
    
    }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料