[英]PHP ZipArchive Removing First Character of Filenames
The code below successfully creates a zip file of a specified directory and all sub-directories.下面的代码成功创建了一个指定目录及其所有子目录的zip文件。 The problem I am having is that the end result removes the first character from each filename and folder-name within the root directory;
我遇到的问题是最终结果从根目录中的每个文件名和文件夹名中删除了第一个字符; this same behavior does not occur in the sub-directories.
在子目录中不会发生相同的行为。
<?php
require('../config/config.php');
$rootPath = $abs_path.'/includes/qrcodes/'; // SOURCE FOLDER TO ZIP
$zipFilename = 'qr_images.zip';
$zip = new ZipArchive();
$zip->open($zipFilename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($rootPath), RecursiveIteratorIterator::LEAVES_ONLY);
foreach ($files as $name => $file)
{
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen($rootPath) + 1);
if (!$file->isDir())
{
$zip->addFile($filePath, $relativePath);
}else {
if($relativePath !== false)
$zip->addEmptyDir($relativePath);
}
}
$zip->close();
?>
Currently the resulting zip file includes:目前生成的 zip 文件包括:
The folders should be named "radios", "infosys", and "vehicles".这些文件夹应命名为“radios”、“infosys”和“vehicles”。
The code appears to have been designed for a $rootpath variable without a trailing backslash.该代码似乎是为没有尾随反斜杠的 $rootpath 变量设计的。 If you'd change it
$rootPath = $abs_path.'/includes/qrcodes';
如果你改变它
$rootPath = $abs_path.'/includes/qrcodes';
the +1 at the substr would be required.将需要 substr 处的 +1。 Otherwise the relative path would start with a '/'.
否则,相对路径将以“/”开头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.