I am trying to create a zip file of folder
Folder to which i want make zip is 1030 Wanted out put 1030.zip
$zip_file = '/var/www/html/projectnamestorage/app/public/projectname/1030.zip';
$zip = new \ZipArchive();
$zip->open($zip_file, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$path = '/var/www/html/projectnamestorage/app/public/projectname/1030/originals';
$files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($files as $name => $file)
{
// We're skipping all subfolders
if (!$file->isDir()) {
$filePath = $file->getRealPath();
// extracting filename with substr/strlen
// $relativePath = substr($filePath, strlen($path) + 1);
$relativePath = 'originals/' . substr($filePath, strlen($path) + 1);
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
return response()->download($zip_file);
I am getting this error
ZipArchive::close(): Invalid or uninitialized Zip object
Use the following code.
<?php
$zip = new ZipArchive();
$DelFilePath = "/var/www/html/projectnamestorage/app/public/projectname/1030.zip";
if(file_exists($DelFilePath))
{
unlink ($DelFilePath);
}
if ($zip->open($DelFilePath, ZIPARCHIVE::CREATE) != TRUE)
{
die ("Could not open archive");
}
$zip->addFile("file_path","file_name");
$zip->close();
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.