1

I have the following code running for quite some time now:

$thisdir = "$_SERVER[DOCUMENT_ROOT]/webroot/uploads/user_uploaded_files/";

    if( !empty( $files ) ){

     $destination = 'uploads/zip_files/meeting_' . $meetingId .'.zip';

     $zip = new ZipArchive();

     $zip->open( $destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );

     //Add all the uploaded files ( $file = filename.ext )

         foreach( $files as $file ){

             if(file_exists( $thisdir . $file )){

                $zip->addFile('/uploads/user_uploaded_files/' . $file, $file);

         }
}

However, it had just recently stopped working (not sure of the previous, but current version is 7.0.9 ).

foreach loop runs as previously, file_exists always returns true, but no files are being added to the archive.

Did anyone experience that too? Any help or guidance is much appreciated.

Domas
  • 1,133
  • 4
  • 18
  • 46

2 Answers2

2

You should probably test if $zip->open worked :

$res = $zip->open($destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE);
if ($res) {
    foreach ($files as $file) {
        if (file_exists($thisdir . $file)) {
            $zip->addFile('/uploads/user_uploaded_files/' . $file, $file);
        }
    }
}

You could add

error_reporting(E_ALL);
ini_set('display_errors', true);

at the beginning of your script, and see if there is any error.

vincenth
  • 1,732
  • 5
  • 19
  • 27
0

Just found out a solution to this problem and hope that it might help someone in the future.

$files was an array of strings, thus looping over it like following : foreach( $files as $file ) I was always getting a single string.

At least I thought so, however addFile() did not apparently read it as string.

The problem was solved by simply casting every single item before addFile(). Solution looks like this:

foreach( $files as $file ){

    if(file_exists( $thisdir . $file )){

        $file = (string)$file;

        $zip->addFile('uploads/user_files/' . $file, $file);

    }

}
Domas
  • 1,133
  • 4
  • 18
  • 46