Zip 功能在服务器上无法正常工作
关于为什么这在我的本地主机中完美运行但在我上传到的服务器中不起作用的任何想法?在服务器中,它创建 zip 但不创建文件夹,它将所有文件放入 .zip 中,不区分文件夹。
function rzip($source, $destination) {
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open($destination, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source));
foreach ($iterator as $key=>$value) {
$new_filename = substr($key,strrpos($key,"/") + 1);
$zip->addFile(realpath($key), $new_filename) or die ("ERROR: Could not add file: $key");
}
$zip->close();
}
Any ideas on why this is perfectly working in my localhost but not in the server where I uploaded it to? In the server, it creates the zip but does not create the folders, it puts all the files inside the .zip, with no folders distinction.
function rzip($source, $destination) {
// create object
$zip = new ZipArchive();
// open archive
if ($zip->open($destination, ZIPARCHIVE::CREATE) !== TRUE) {
die ("Could not open archive");
}
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source));
foreach ($iterator as $key=>$value) {
$new_filename = substr($key,strrpos($key,"/") + 1);
$zip->addFile(realpath($key), $new_filename) or die ("ERROR: Could not add file: $key");
}
$zip->close();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在某些地方错误使用(或未使用)
RecursiveDirectoryIterator
。第一点是您将迭代点文件夹(
.
和..
),这可能是不需要的;要停止这种情况,请使用SKIP_DOTS
标志。接下来,有一些工具可以获取文件相对于正在迭代的主目录的路径,并获取真实路径;分别使用
getSubPathname()
和getRealpath()
方法。上面只是一个答案,因为它太长了,无法发表评论。上面没有回答为什么,“这在我的本地主机中完美工作,但在服务器中不起作用”。
You are mis-using (or not using) the
RecursiveDirectoryIterator
in places.The first point is that you will iterate over the dot folders (
.
and..
) which is probably undesired; to stop this, use theSKIP_DOTS
flag.Next, there are tools to get the file's path relative to the main directory being iterated over and to get the real path too; using the
getSubPathname()
andgetRealpath()
methods, respectively.The above is only an answer because it's too long for a comment. Nothing above answers why, "this is perfectly working in my localhost but not in the server".