php - 从目录创建 zip 存档时如何删除根路径?
问题描述
我正在从mydir
存储在/my/files/inside/here/
.
我希望存档mydir.zip
仅包含来自mydir
. 然而,问题是/my/files/inside/here/mydir/
当我解压缩时我现在得到了完整的根路径..
$mypath = "/my/files/inside/here/";
$dirname_to_zip = "mydir";
$zip = new ZipArchive();
$zipfile_with_path = $mypath.$dirname_to_zip.".zip";
if ($zip->open($zipfile_with_path, ZipArchive::CREATE)!==TRUE)
die("failed to create zip");
// Create recursive directory iterator
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($mydir.$dirname_to_zip),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($dirname_to_zip) );
// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
$zip->close();
如何删除根路径?