2015-04-03 80 views
0

其實我有zipfile。但我並不需要桌面上的全部文件。我想要選擇userid文件...我嘗試編寫下面的代碼,我使用了php和mysql。如何從數據庫中下載多個文件在php中

<?php 

    function zipFilesAndDownload($file_names,$archive_file_name,$file_path) 
    { 
     $zip = new ZipArchive(); 
     if ($zip->open($archive_file_name, ZIPARCHIVE::CREATE)!==TRUE) { 
      exit("cannot open <$archive_file_name>\n"); 
     } 

     foreach($file_names as $files) 
     { 
      $zip->addFile($file_path.$files,$files); 
      //echo $file_path.$files,$files."<br />"; 
     } 
     $zip->close(); 
     header("Content-type: application/zip"); 
     header("Content-Disposition: attachment; filename=$archive_file_name"); 
     header("Pragma: no-cache"); 
     header("Expires: 0"); 
     readfile("$archive_file_name"); 
     exit; 
    } 
    require_once("config.php"); 
    $cqurfetch=mysql_query("SELECT * FROM albums where user_id='$user_id' and accept='1'"); 
    while($row = mysql_fetch_array($cqurfetch)) 
    { 
     $file_names[] = $row['user_album_images']; 
    } 
     $archive_file_name=time().'.gallery.zip'; 
     $file_path="/uploads/"; 
     zipFilesAndDownload($file_names,$archive_file_name,$file_path); 
?> 
+0

$ file_names array你得到了什麼?所有文件都只有$ user_id文件。 – RaMeSh 2015-04-03 11:57:27

+0

可以請你詳細說明你的問題 – RaMeSh 2015-04-03 12:08:18

+0

我只想要$ user_id文件,不是所有文件 – 2015-04-03 12:11:07

回答

0

很多小徑後,我有你的問題的一個答案。

只有'user_id'文件被壓縮並存儲在該zip文件夾中。

例子: -

<?php 
require_once("config.php"); 

    $user_id = 49 ; //Just For your reference 
    $accept = 1 ;  //Just For your reference 

    $sql = "SELECT * FROM albums where user_id='$user_id' and accept='$accept'"; 
    $result = $con->query($sql); 

    if ($result->num_rows > 0) { 
    // output data of each row 
    while($row = $result->fetch_assoc()) { 
    $array[] = $row["user_album_images"]; 
    echo "user_album_images: " .$row["user_album_images"]. "<br>"; //This is also just for your reference 
    } 
    } 
    else{ 
    echo "fail"; 
    } 

$file= "/uploads/gallery.zip"; //Zip file path 
$zip = new ZipArchive; 
echo "*****zip started.*****\n"; 
    if ($zip->open($file, ZipArchive::CREATE) === TRUE) { 
    foreach($array as $key => $value) 
{ 
    $zip->addFile("/".$value); 
      } 
    $zip->close(); 
    echo '^^^^^^Zip ended^^^^^^'; 
    } 
?> 

我在本地系統測試此代碼,這是工作像您的要求。

希望這會有所幫助