为了账号安全,请及时绑定邮箱和手机立即绑定

PHP ZipArchive 删除文件名的第一个字符

PHP ZipArchive 删除文件名的第一个字符

PHP
慕妹3242003 2023-04-15 10:49:06
下面的代码成功创建了指定目录和所有子目录的 zip 文件。我遇到的问题是最终结果从根目录中的每个文件名和文件夹名中删除了第一个字符;同样的行为不会发生在子目录中。<?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(); ?>目前生成的 zip 文件包括:再见(文件夹)radio_1.pngradio_2.pngnfosys(文件夹)infosys_1.pngehicles(文件夹)vehicle_1.pngvehicle_2.png这些文件夹应命名为“radios”、“infosys”和“vehicles”。
查看完整描述

1 回答

?
慕容3067478

TA贡献1773条经验 获得超3个赞

该代码似乎是为没有结尾反斜杠的 $rootpath 变量设计的。如果您要更改它,$rootPath = $abs_path.'/includes/qrcodes';则需要 substr 处的 +1。否则,相对路径将以“/”开头。



查看完整回答
反对 回复 2023-04-15
  • 1 回答
  • 0 关注
  • 68 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信