Upload all the files in the directory from my website to a ZIP folder

That's it, I allow users to upload images to my site. For this user, I would like to upload all the images that the user uploaded to my site from my site. Therefore, I would like to basically have a disclosure of user names, and then when I select one query in my database and get all the images they upload. This part is not a problem.

My problem is how can I go through each of these files and put them in a zip folder and then download the zip folder (if possible).

Any ideas on how to do something like this?

Thanks in advance!

EDIT: I know how to upload a file after flashing it using the following code:

header('Content-Type: application/zip'); header('Content-disposition: attachment; filename=filename.zip'); header('Content-Length: ' . filesize($zipfilename)); readfile($zipname); 
+4
source share
3 answers

Thanks to the help of @maxhud, I was able to come up with a complete solution. Here is the final piece of code 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); } ?> 
+2
source

PHP command that allows you to run a system command

And a system command like

more effective.

EX of me backs up the file system and overwrites 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 the headers to send an error

+1
source

All Articles