我在一个文件夹中有几个ziped文件。我想将它们解压缩到指定的文件夹。我有以下PHP代码:
$path = "docs/" . $ID;
$files = scandir("temp" . '/' . $ID );
foreach ($files as $athely){
$zip = new ZipArchive;
$res = $zip->open($athely);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $athely";
}
}
它不起作用。我做错了什么?
答案 0 :(得分:1)
问题是您没有使用完整路径打开zip。另一件需要注意的事情是,如果多个zip文件具有相同名称的文件夹,则一个文件夹将覆盖另一个文件夹。
<?php
$path = "docs/" . $ID;
$files = scandir("temp" . '/' . $ID );
foreach ($files as $athely){
if($athely=="." || $athely=="..") continue;
$target_path = "temp/".$ID."/".$athely;
$file = $athely;
$zip = new ZipArchive;
$res = $zip->open($target_path);
if ($res === TRUE) {
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $athely";
}
}
?>