[英]PHP Create Zip file
I am trying to create a zip file of folder我正在尝试创建文件夹的 zip 文件
Folder to which i want make zip is 1030 Wanted out put 1030.zip我要制作 zip 的文件夹是 1030 想要输出 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 ZipArchive::close(): 无效或未初始化 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();
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.