How to set output directory for the created zip archive using PHP ZipArchive class?

55 views Asked by At

I am using PHP ZipArchive class to archive files from different locations. Code is below

php

$files = array('path/1/file1.zip', 'path/2/file2.zip');
$zipname = "path/to/output/archive-" . time() . ".zip"; // Zip name

$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
    
    $data = file_get_contents($file);
    
    $name =  basename($file, '.zip') . '.zip';
    if (file_exists($path)) {
        $zip->addFromString($name,  $data);
        
    } else {
        echo "file does not exist";
        exit;
    }
}
$zip->close();

Problem is it creates a zip file with name path_to_output_archive-16783322.zip file in root directory. I was expecting it to create archive-16783322.zip inside path/to/output/ folder.

0

There are 0 answers