CL2*_*L22 6 php windows ziparchive
我没有把一个文件放到一个新的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();
Run Code Online (Sandbox Code Playgroud)
zip已创建,但为空.然而,没有触发任何错误.
出了什么问题?
spe*_*bus 13
在某些配置中,localname当将文件添加到zip存档时,PHP无法正常运行,并且必须手动提供此信息.因此,使用第二个参数addFile()可能会解决此问题.
ZipArchive :: addFile
参数
- filename
要添加的文件的路径.- localname
如果提供,这是ZIP存档中将覆盖文件名的本地名称.
$zip->addFile(
$fileToZip,
basename($fileToZip)
);
Run Code Online (Sandbox Code Playgroud)
您可能必须调整代码以获得正确的树结构,因为basename()将从文件名中删除除路径之外的所有内容.