加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:50:58 所属栏目:PHP教程 来源:网络整理
导读:我没有把一个文件放到一个新的zip存档中. makeZipTest.php: ?php$destination = __DIR__.'/makeZipTest.zip';$fileToZip = __DIR__.'/hello.txt';$zip = new ZipArchive();if (true !== $zip-open($destination,ZIPARCHIVE::OVERWRITE)) { die("Problem ope
我没有把一个文件放到一个新的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()将从文件名之外的路径中删除所有内容.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读