【问题标题】:Download all Files in a Directory from my Website into a Zip Folder将目录中的所有文件从我的网站下载到 Zip 文件夹中
【发布时间】:2012-07-10 19:31:42
【问题描述】:

所有, 我允许用户将图像上传到我的网站。对于该用户,我想下载用户从我的网站上传到我的网站的所有图像。所以我想基本上有一个用户名的下拉列表,然后当我选择一个查询我的数据库并获取他们下载的所有图像时。那部分没有问题。

我的问题是,我如何浏览每个文件并将它们放入一个 zip 文件夹,然后下载该 zip 文件夹(如果可能的话)。

关于如何做这样的事情有什么想法吗?

提前致谢!

编辑:我知道使用以下代码压缩文件后如何下载文件:

header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zipfilename));
readfile($zipname);

【问题讨论】:

标签: php


【解决方案1】:

【讨论】:

  • 我根据您的第一个链接提供的信息创建了我的 zip 文件(谢谢!)。但是我用代码更新了我的答案以下载 zip 文件,但我不确定如何填充这些值。我想用我刚刚根据第一个链接创建的 zip 填充它们。你对如何做到这一点有任何帮助吗?
【解决方案2】:

感谢@maxhud 的帮助,我能够想出完整的解决方案。这是用于实现我想要的结果的最终代码 sn-p:

<?php
/* creates a compressed zip file */
function create_zip($files = array(),$destination = '',$overwrite = true) {
  //if the zip file already exists and overwrite is false, return false
  if(file_exists($destination) && !$overwrite) { return false; }
  //vars
  $valid_files = array();
  //if files were passed in...
  if(is_array($files)) {
    //cycle through each file
    foreach($files as $file) {
      //make sure the file exists
      if(file_exists($file)) {
        $valid_files[] = $file;
      }
    }
  }
  //if we have good files...
  if(count($valid_files)) {
    //create the archive
    $zip = new ZipArchive();
    if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
      return false;
    }
    //add the files
    foreach($valid_files as $file) {
      $zip->addFile($file,$file);
    }
    //debug
    //echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

    //close the zip -- done!
    $zip->close();

    //check to make sure the file exists
    return file_exists($destination);
  }
  else
  {
    return false;
  }
}



$files_to_zip = array(
  'upload/1_3266_671641323389_14800358_42187034_1524052_n.jpg', 'upload/1_3266_671641328379_14800358_42187035_3071342_n.jpg'
);
//if true, good; if false, zip creation failed
$zip_name = 'my-archive.zip';
$result = create_zip($files_to_zip,$zip_name);

if($result){
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=filename.zip');
header('Content-Length: ' . filesize($zip_name));
readfile($zip_name);
}
?>

【讨论】:

    【解决方案3】:

    一个可以让你运行系统命令的 PHP 命令

    还有一个系统命令,比如

    一般来说效率更高。

    我创建一个文件系统的备份并覆盖以前的备份

    $uploads = wp_upload_dir();
    $file_name  = 'backup_filesystem.tar.gz';
    unlink($uploads['basedir'] . '/' . $file_name);
    
    ob_start();
    $output = shell_exec(sprintf('tar -zcvf %s/%s %s', $uploads['basedir'], $file_name, ABSPATH));
    ob_end_clean();
    

    注意:输出缓冲区,以防你的 php to shell 命令有输出并且你不希望标头已经发送错误

    【讨论】:

    • 我不确定,我认为这是一个很好的建议。我不是对你的答案投反对票的人。
    猜你喜欢
    • 1970-01-01
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    • 2018-12-26
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    相关资源
    最近更新 更多