在PHP中,这个错误意味着什么:Warning:ZipArchive :: close():读取错误:错误的文件描述符在(...)中?
问题描述:
我有一个PHP脚本,使拉链带2个功能:在PHP中,这个错误意味着什么:Warning:ZipArchive :: close():读取错误:错误的文件描述符在(...)中?
- dirToArray:让所有的文件/ empty_folders阵列中的
- create_zip:调用dirToArray(),使zipArchive
我得到了一个奇怪的警告,它实际上是一个真正的错误,因为我的zip档案没有被构建。
警告结果
Warning: ZipArchive::close(): Read error: Bad file descriptor in path/to/file.php on line x
有人可以解释我是什么意思: “错误的文件描述符”?
这是代码:
dirToArray
/* to copy all file/folder names from a directory into an array*/
function dirToArray($dir_path) {
$result = array();
$path = realpath($dir_path);
$objects = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path), \RecursiveIteratorIterator::SELF_FIRST);
foreach($objects as $name => $object) {
if($object->getFilename() !== "." && $object->getFilename() !== "..") {
$result[] = $object;
}
}
return $result;
}
create_zip
/* creates a compressed zip file */
function create_zip($productPath = '', $dirName = '', $overwrite = false) {
$fullProductPath = $productPath.$dirName;
$a_filesFolders = dirToArray($fullProductPath);
var_dump($a_filesFolders);
//if the zip file already exists and overwrite is false, return false
$zip = new \ZipArchive();
$zipProductPath = $fullProductPath.'.zip';
if($zip->open($zipProductPath) && !$overwrite){
$GLOBALS["errors"][] = "The directory {$zipProductPath} already exists and cannot be removed.";
}
//if files were passed in...
if(is_array($a_filesFolders) && count($a_filesFolders)){
$opened = $zip->open($zipProductPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
if($opened !== true){
$GLOBALS["errors"][] = "Impossible to open {$zipProductPath} to edit it.";
}
//cycle through each file
foreach($a_filesFolders as $object) {
//make sure the file exists
$fileName = $object -> getFilename();
$pathName = $object -> getPathname();
if(file_exists($pathName)) {
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
echo $pathName.'<br/>';
echo $fileDestination.'<br/>';
$zip->addFile($pathName,$fileDestination);
}
else if(is_dir($pathName)){
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
$zip->addEmptyDir($fileDestination);
}else{
$GLOBALS["errors"][] = "the file ".$fileName." does not exist !";
}
}
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($zipProductPath);
}else{
return false;
}
}
答
我发现这个问题......我用file_exists()混淆函数检测是否存在目录...所以脚本将文件夹添加为文件并发出错误。
create_zip(修补)
/* creates a compressed zip file */
function create_zip($productPath = '', $dirName = '', $overwrite = false) {
$fullProductPath = $productPath.$dirName;
$a_filesFolders = dirToArray($fullProductPath);
var_dump($a_filesFolders);
//if the zip file already exists and overwrite is false, return false
$zip = new \ZipArchive();
$zipProductPath = $fullProductPath.'.zip';
if($zip->open($zipProductPath) && !$overwrite){
$GLOBALS["errors"][] = "The directory {$zipProductPath} already exists and cannot be removed.";
return false;
}
//if files were passed in...
if(is_array($a_filesFolders) && count($a_filesFolders)){
$opened = $zip->open($zipProductPath, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
if($opened !== true){
$GLOBALS["errors"][] = "Impossible to open {$zipProductPath} to edit it.";
return false;
}else{
//cycle through each file
foreach($a_filesFolders as $object) {
//make sure the file exists
$fileName = $object -> getFilename();
$pathName = $object -> getPathname();
if(is_dir($pathName)){ /*<-- I put on first position*/
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
$zip->addEmptyDir($fileDestination);
}else if(file_exists($pathName)) {
$pos = strpos($zipProductPath , "/tmp/") + 5;
$fileDestination = substr($pathName, $pos);
$zip->addFile($pathName,$fileDestination);
}
else{
$GLOBALS["errors"][] = "the file ".$fileName." does not exist !";
}
}
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($zipProductPath);
}
}else{
return false;
}
}
我的猜测是,打电话给'的open()'失败,然后您尝试关闭描述符,因为它一直没有拉开 –
我不存在检查:if($ opened!== true){GLOBALS [“errors”] [] =“无法打开{$ zipProductPath}进行编辑。 } –
是的,但它不会停止脚本的执行 –