-3

Folder called "active" full of JPG files that I want to compress to "active.zip".

I am using PHP 8.0 and currently have no compression libraries installed.

In Python this would simply be:

shutil.make_archive("active", 'zip', "active")

No equivalent one-liner in PHP?

Ned Hulton
  • 477
  • 3
  • 12
  • 27
  • https://www.php.net/manual/en/class.ziparchive.php – ADyson Apr 26 '22 at 20:40
  • @ADyson No, I reviewed that already. The code failed to work. – Ned Hulton Apr 26 '22 at 20:42
  • Which code? There are multiple answers. And failed in what way exactly? The ZipArchive class works fine. In order for your question not to be considered a duplicate you'll need to show us a [mre] of an issue which can't be resolved by one of those answers. There's also [all of these](https://www.google.com/search?q=php+zip+a+folder) too. – ADyson Apr 26 '22 at 20:43
  • @ADyson I understand, but the person who posted that wants to do something different: "after zipping the My Folder, i also want to delete the whole content of the folder except important.txt". All I want is to zip a folder. I hear what you are saying, but there is nothing online explaining how to do this straightforward thing and the community would benefit I think. – Ned Hulton Apr 26 '22 at 20:45
  • 1
    "after zipping the My Folder, i also want to delete the whole content of the folder except important.txt"...well you can just copy the bit before that which creates the zip, and ignore the code which does the deletion. The accepted answer already splits up the code like this anyway. Or you can use an example from one of the many other links available when you google "php zip a folder" – ADyson Apr 26 '22 at 20:47

1 Answers1

1

Apparently, there is no one-liner, went with this:

// Enter the name of directory
$pathdir = "backups/active/"; 
  
// Enter the name to creating zipped directory
$zipcreated = "backups/active.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();
}
Ned Hulton
  • 477
  • 3
  • 12
  • 27