[英]PHP ZipArchive is not adding any files (Windows)
I'm failing to put even a single file into a new zip archive. 我没有把一个文件放到一个新的zip存档中。
makeZipTest.php: 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();
The zip gets created, but is empty. zip已创建,但为空。 Yet no errors are triggered.
然而,没有触发任何错误。
What is going wrong? 出了什么问题?
It seems on some configuration, PHP fails to get the localname
properly when adding files to a zip archive and this information must be supplied manually. 在某些配置中,当将文件添加到zip存档时,PHP无法正确获取
localname
,并且必须手动提供此信息。 It is therefore possible that using the second parameter of addFile()
might solve this issue. 因此,使用
addFile()
的第二个参数可能会解决此问题。
ZipArchive::addFile
ZipArchive :: addFile
Parameters
参数
- filename
文档名称
The path to the file to add.要添加的文件的路径。
- localname
的localName
If supplied, this is the local name inside the ZIP archive that will override the filename.如果提供,这是ZIP存档中将覆盖文件名的本地名称。
PHP documentation: ZipArchive::addFile PHP文档:ZipArchive :: addFile
$zip->addFile(
$fileToZip,
basename($fileToZip)
);
You may have to adapt the code to get the right tree structure since basename()
will remove everything from the path apart from the filename. 您可能必须调整代码以获得正确的树结构,因为
basename()
将从文件名之外的路径中删除所有内容。
You need to give server right permission in folder where they create zip archive. 您需要在创建zip存档的文件夹中授予服务器权限。 You can create
tmp
folder with write permision chmod 777 -R tmp/
您可以使用write permision创建
tmp
文件夹chmod 777 -R tmp/
Also need to change destination where script try to find hello.txt
file $zip->addFile($fileToZip, basename($fileToZip))
还需要更改目标,其中脚本尝试查找
hello.txt
文件$zip->addFile($fileToZip, basename($fileToZip))
<?php
$destination = __DIR__.'/tmp/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, basename($fileToZip))) {
die("Could not add file $fileToZip");
}
echo "numfiles: " . $zip->numFiles . "\n";
echo "status: " . $zip->status . "\n";
$zip->close()
check this class to add files and sub-directories in a folder to zip file,and also check the folder permissions before running the code, ie chmod 777 -R zipdir/ 检查这个类将文件夹中的文件和子目录添加到zip文件中,并在运行代码之前检查文件夹权限,即chmod 777 -R zipdir /
HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
<?php
class HZip
{
private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir($folder);
while (false !== $f = readdir($handle)) {
if ($f != '.' && $f != '..') {
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $exclusiveLength);
if (is_file($filePath)) {
$zipFile->addFile($filePath, $localPath);
} elseif (is_dir($filePath)) {
// Add sub-directory.
$zipFile->addEmptyDir($localPath);
self::folderToZip($filePath, $zipFile, $exclusiveLength);
}
}
}
closedir($handle);
}
public static function zipDir($sourcePath, $outZipPath)
{
$pathInfo = pathInfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$z = new ZipArchive();
$z->open($outZipPath, ZIPARCHIVE::CREATE);
$z->addEmptyDir($dirName);
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
$z->close();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.