Search code examples
phpphp-ziparchive

Get the Files from a directory and compress them into a ZIP-Package


Short explanation:

There are some files in the directory

Yii::app()->runtimePath.'/temp/myDir/';

These files should be zipped in the same directory like:

Yii::app()->runtimePath.'/temp/myDir/files.zip

Following construction gives no errors, but zip-file is not created.

$zip = new ZipArchive();
$path = Yii::app()->runtimePath.'/temp/myDir/';

// Open an empty ZIP-File to write into
$ret = $zip->open('files.zip', ZipArchive::OVERWRITE);

if ($ret !== TRUE) {
    printf('Failed with code %d', $ret);
} else {
    $options = array('add_path' => $path, 'remove_all_path' => TRUE);
    $zip->addGlob('*.{png,gif,jpg,pdf}', GLOB_BRACE, $options);
    $res = $zip->close();
}

What is my mistake? Directory name is correct and it is writable (CHMOD 777).


Solution

  • this works (excerpt):

    $ret = $zip->open($path.'files.zip', ZipArchive::OVERWRITE);
    ...
    $options = array('remove_all_path' => TRUE);
    $zip->addGlob($path.'*.{png,gif,jpg,pdf}', GLOB_BRACE, $options);