我无法创建zip文件。它给了我这个错误。 search没有结果。这是我的功能。
function CompressFile($fromExtractedFile)
{
$archive = new ZipArchive;
if ($archive->open($fromExtractedFile.".zip", ZipArchive::OVERWRITE | ZipArchive::CREATE) === TRUE) {
$archive->addFile($fromExtractedFile, 'payload.csv');
$archive->close();
}
else {
throw new Exception ("Compress operation to ZIP file failed. $fromExtractedFile -> $fromExtractedFile.zip");
}
return $fromExtractedFile.".zip";
}
控制台:
Compressing /tmp/bing_PeUYiT
PHP Warning 'yii\base\ErrorException' with message 'ZipArchive::close(): Renaming temporary file failed: No such file or directory'
文档:http://php.net/manual/en/book.zip.php
PHP 5.6,Win 8.1,Cygwin 2.5.2
PsySh中也是如此:
>>> $archive = new ZipArchive
=> ZipArchive {#199
+"status": 0,
+"statusSys": 0,
+"numFiles": 0,
+"filename": "",
+"comment": "",
}
>>> $archive->open('/tmp/bing_PeUYiT.zip', ZipArchive::OVERWRITE|ZipArchive::CREATE)
=> true
>>> $archive->addFile('/tmp/bing_PeUYiT', 'payload.csv')
=> true
>>> $archive->close()
PHP warning: ZipArchive::close(): Renaming temporary file failed: No such file or directory on line 1
答案 0 :(得分:0)
由于ZipArchive
已被破坏,我改为使用PclZip。
$ composer需要pclzip / pclzip
function compressFile2($file) {
$zip = new PclZip($file.'.zip');
$error = $zip->create([[PCLZIP_ATT_FILE_NAME => $file, PCLZIP_ATT_FILE_NEW_FULL_NAME => 'payload.csv']]); # array of array
#$error = $zip->create($file, PCLZIP_OPT_REMOVE_ALL_PATH);
if ($error == 0) trigger_error("Error : ".$zip->errorInfo(true));
return $file.'.zip';
}