How to zip a folder with php

Asked

Viewed 35 times

1

I’m trying to zip the briefcase img to play in the folder zipped, but I’m not succeeding.

Would you like to know what the mistake is? Because I’ve seen some tutorials and I can’t zip

    $diretorio = getcwd().'/zipado/'; //para onde vou mandar

    $zip = new ZipArchive(); 

    if($zip->open('nome_arquivo_zip.zip', ZIPARCHIVE::CREATE) == TRUE){
        $zip->addFile($diretorio.'teste.zip', getcwd().'/img/');
    }
  • Is the application returning any error? Or the file is not being saved only?

  • The file is not being saved

  • Try to put after this addFile, the following code: $zip->close();

  • Have you seen the parameters that method addFile receives? The first is the filename of the file to be added and you are passing the name of the Zip itself.

1 answer

0

The file may not be being saved because Ziparchive is not being "closed" after adding the files. To save the file, just add the following code below the addFile();

$diretorio = getcwd().'/zipado/'; //para onde vou mandar

$zip = new ZipArchive(); 

if($zip->open('nome_arquivo_zip.zip', ZIPARCHIVE::CREATE) == TRUE){
    $zip->addFile(getcwd().'/img/');
    $zip->close();
}

The close() method saves the file and makes the change based on the added/removed Ziparchive Object items.

  • Vinicius nothing either

Browser other questions tagged

You are not signed in. Login or sign up in order to post.