微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PHP ZipArchive没有添加任何文件(Windows)

我没有把一个文件放到一个新的zip存档中.

makeZipTest.PHP

<?PHP

$destination = __DIR__.'/makeZipTest.zip';
$filetoZip = __DIR__.'/hello.txt';

$zip = new ZipArchive();
if (true !== $zip->open($destination,ZIPARCHIVE::OVERWRITE)) {
    die("Problem opening zip $destination");
}
if (!$zip->addFile($filetoZip)) {
    die("Could not add file $filetoZip");
}
echo "numfiles: " . $zip->numFiles . "\n";
echo "status: " . $zip->status . "\n";
$zip->close();

zip已创建,但为空.然而,没有触发任何错误.

出了什么问题?

在某些配置中,当将文件添加到zip存档时,PHP无法正确获取本地名称,并且必须手动提供此信息.因此,使用addFile()的第二个参数可能会解决此问题.

ZipArchive::addFile

Parameters

  • filename
    The path to the file to add.
  • localname
    If supplied,this is the local name inside the ZIP archive that will override the filename.

PHP documentation: ZipArchive::addFile

$zip->addFile(
    $filetoZip,basename($filetoZip)
);

您可能必须调整代码以获得正确的树结构,因为basename()将从文件名之外的路径中删除所有内容.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐