php - 如何在php中从数据库中归档下载多个文件

标签 php mysql

实际上我得到了 zip 文件。但我不需要表中的总文件。我想选择用户 ID 文件...我尝试编写下面的代码,我使用了 php 和 mysql。

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

最佳答案

经过多次尝试,我对你的问题有了一个答案。

只有'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^^^^^^';
  }
?>

我已经在本地系统中测试了这段代码,这符合您的要求。

希望这有帮助

关于php - 如何在php中从数据库中归档下载多个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29431662/

相关文章:

php - 如何从 Vue 对 Laravel 执行批量删除?

java - 文章管理网站选择什么

PHP偶数生成

mysql - Rails activerecords 查询,行分割

php - html不在wamp中解析为php

javascript - CORS 不再工作

sql - MySQL如何过滤出多个匹配键的记录?

php - DooPHP 中的复杂 ORM

java - Spring Boot 连接到 AWS RDS MySQL - SSLHandshakeException : Received fatal alert: unknown_ca

MYSQL - 将每个句子中第一个单词的第一个字母大写