简体   繁体   中英

Download all Files in a Directory from my Website into a Zip Folder

All, I allow users to upload images to my website. For that user I'd like to download all of the images that the user uploaded to my site from my website. So I'd like to basically have a dropdown of the usernames and then when I select one query my database and obtain all of the images that they downloaded. That part is no issue.

My issue is that how can I go through each of these files and put them into a zip folder and then download the zip folder (if that's possible).

Any ideas on how to go about doing something like that?

Thanks in advance!

EDIT: I know how to download the file once it's zipped by using the following code:

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

Thanks to the help from @maxhud I was able to come up with the complete solution. Here is the final code snippet used to achieve my desired results:

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

A PHP command that will let you run a system command

AND a system command like

is more efficient generally.

EX of me creating a backup of a filesystem and overwriting the previous backup

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

note: output buffer in case your php to shell command has output and you don't want a headers already sent error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM