[英]ZIP file not being created, but no error gets triggered
I'm using ZipArchive:我正在使用 ZipArchive:
function zip_dir($source, $target){
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
$zip = new \ZipArchive();
if($zip->open($target, \ZipArchive::CREATE) !== true)
exit('cannot create zip');
foreach($iterator as $file){
$zip->addFile($file);
print $file . '<br>';
}
$zip->close();
return $target;
}
zip_dir(__DIR__ . '/test/', __DIR__ . '/testarchive.zip');
I can see the list of files, but in the end I cannot find the zip file that's supposed to be created.我可以看到文件列表,但最终我找不到应该创建的 zip 文件。 And I get no errors / exceptions from ZipArchive...
而且我没有从 ZipArchive 中得到任何错误/异常...
edit:编辑:
I've added print $zip->getStatusString();
我添加了
print $zip->getStatusString();
after $zip->close();
在
$zip->close();
and it prints :Can't open file: Permission denied". What does that mean? I know for sure every directory is writable, bc I can create new files with PHP inside them...它打印:无法打开文件:权限被拒绝”。这是什么意思?我确定每个目录都是可写的,bc 我可以在其中创建带有 PHP 的新文件......
edit 2:编辑2:
if(is_writable(dirname($target)))
print 'target dir is writable...';
it prints that, so the dir is writable.它打印出来,所以目录是可写的。 Feels like I'm in the twilight zone...
感觉就像我在暮光之城...
Two Comments From php.net来自 php.net 的两条评论
If you're adding multiple files to a zip and your $zip->close()
call is returning FALSE, ensure that all the files you added actually exist.如果您将多个文件添加到一个 zip 文件并且您的
$zip->close()
调用返回 FALSE,请确保您添加的所有文件确实存在。 Apparently $zip->addFile()
returns TRUE even if the file doesn't actually exist.显然,即使文件实际上不存在,
$zip->addFile()
也会返回 TRUE。 It's a good idea to check each file with file_exists()
or is_readable()
before calling $zip->addFile()
on it.在调用
$zip->addFile()
之前使用file_exists()
或is_readable()
检查每个文件是个好主意。
and和
Don't forget to check the zip isn't empty, folks - otherwise the zip won't be created at all, and the server will issue no warning!不要忘记检查 zip 是否为空,伙计们 - 否则根本不会创建 zip,并且服务器不会发出警告!
Sounds like you have a permission issue, either with writing to the zip file, or reading the files it is zipping.听起来您有权限问题,无论是写入 zip 文件,还是读取正在压缩的文件。
I would use a combination of file_exists
, is_readable
, and is_writable
to figure out which of these is causing the problem.我会使用
file_exists
、 is_readable
和is_writable
的组合来找出其中哪一个导致了问题。
function zip_dir($source, $target){
$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($source, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
$zip = new \ZipArchive();
if($zip->open($target, \ZipArchive::CREATE) !== true)
exit('cannot create zip');
foreach($iterator as $file){
if (!file_exists($file)) { die($file.' does not exist'); }
if (!is_readable($file)) { die($file.' not readable'); }
$zip->addFile($file);
print $file . '<br>';
}
$zip->close();
return $target;
}
if (!is_writable(__DIR__)) { die('directory not writable'); }
zip_dir(__DIR__ . '/test/', __DIR__ . '/testarchive.zip');
Make sure that:确保:
file_exists()
and is_readable()
before calling $zip->addFile()
).$zip->addFile()
之前使用file_exists()
和is_readable()
检查每个文件)。.
.
and ..
...
$zip['numFiles'] > 0
).$zip['numFiles'] > 0
)。$zip->close()
returns TRUE
.$zip->close()
返回TRUE
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.