ZIP文件没有被创建,但没有错误被触发 [英] ZIP file not being created, but no error gets triggered
问题描述
函数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('can not create zip');
$ b foreach($ iterator as $ file){
$ zip-> addFile($ file);
打印$文件。 <峰; br>;
}
$ zip-> close();
return $ target;
$ b zip_dir(__ DIR__。'/ test /',__DIR__。'/testarchive.zip');
我可以看到文件列表,但最终我无法找到所需的zip文件被创建。我从ZipArchive中得到了任何错误/异常...
$ b $ p
$ b
我添加了 $ zip-> close();
$ b
,它打印:无法打开文件:权限被拒绝
这是什么意思?我知道每个目录是可写的,BC我可以用PHP创建新的文件里面他们... ...
编辑2:
pre $ if(is_writable( dirname($ target)))
print'target dir is writable ...';
如果您将多个文件添加到一个zip文件,并且$ zip-> close()调用返回FALSE,请确保您添加的所有文件实际上是存在的,显然$ zip-> addFile()返回TRUE,即使这个文件实际上不存在,最好用fil检查每个文件在调用$ zip-> addFile()之前调用e_exists()或is_readable()。
$ b 和
不要忘记检查邮编不是空的,乡亲 - 否则邮编将不会被创建,并且服务器将不会发出警告!
I'm using 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. And I get no errors / exceptions from ZipArchive...
edit:
I've added print $zip->getStatusString();
after $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...
edit 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
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. Apparently $zip->addFile() returns TRUE even if the file doesn't actually exist. It's a good idea to check each file with file_exists() or is_readable() before calling $zip->addFile() on it.
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文件没有被创建,但没有错误被触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!