0

Since my file manager doesn't allow me to download multiple files, instead only allowing me to download them one by one (which is tedious, and, eventually will become inefficient), I want to know how to download all my website file contents into a single zip folder. I found a code that works from geeksForGeeks, however it only zips on that current directory level (not recursively). I want every file on my website put into a zip folder while preserving their place in their corresponding folders.

The code I found:

                // Enter the name of directory 
                $pathdir = "./";  
                // Enter the name to creating zipped directory 
                $zipcreated = "BackupFiles.zip"; 
                // Create new zip class 
                $zip = new ZipArchive;   
                if($zip -> open($zipcreated, ZipArchive::CREATE ) === TRUE) {   
                    // Store the path into the variable 
                    $dir = opendir($pathdir); 
                    while($file = readdir($dir)) { 
                        if(is_file($pathdir.$file)) { 
                            $zip -> addFile($pathdir.$file, $file);
                        } 
                    }
                    $zip ->close(); 
                } 

How do I add the folders as well? It only zips the files of the current directory and not all the subfolders.

Reality
  • 637
  • 1
  • 6
  • 25
  • Can you give a rough idea of your host environment? – Progrock Oct 08 '20 at 16:20
  • I've shell exec'd to create a compressed archive in the past, and then have just downloaded the file via http. – Progrock Oct 08 '20 at 16:22
  • My host enviroment? Like what files and folders are in there? The main folder I want to zip includes all the files for my website. I have subfolders containing images, json, and text for neatness – Reality Oct 08 '20 at 16:23
  • I was really thinking something like if the OS is linux, you may have access to tar and gzip, basically slip out of Php and use those tools. – Progrock Oct 08 '20 at 16:24
  • I'm using an online hosting site. I'm using infinityfree.net (may not be the best hosting service, but since I'm new, I don't wanna waste money for mistakes). I can guess it's run on linux, although I don't know. – Reality Oct 08 '20 at 16:25
  • we also don't have access to shell commands at all so... – Reality Oct 08 '20 at 16:27
  • You can see if you can use shell_exec, exec from php with something like: `echo shell_exec('ls');` – Progrock Oct 08 '20 at 16:36
  • 1
    no, I know because the web hosting admin told us we cannot use it. I can try though. edit: Just as I thought: Warning: shell_exec() has been disabled for security reasons – Reality Oct 08 '20 at 16:38
  • Failing that try here: https://stackoverflow.com/a/1334949/3392762 – Progrock Oct 08 '20 at 16:42
  • It says you have to have a zip extension. I don't know if it's already installed, but if not I don't think I can (because I would need shell commands or another form they don't allow). I'll try and let you know, thanks. – Reality Oct 08 '20 at 16:45
  • Works. Thank you so much! – Reality Oct 08 '20 at 16:53
  • The weight on the other answer signifies gratitude. The magic is in those recursive iterators, once you can explain them clearly, please write up your own answer! – Progrock Oct 08 '20 at 22:31

0 Answers0