【问题标题】:In PHP, what means this error : Warning: ZipArchive::close(): Read error: Bad file descriptor in (...)?在 PHP 中,这个错误是什么意思: 警告:ZipArchive::close(): Read error: Bad file descriptor in (...)?
【发布时间】:2018-02-09 11:41:29
【问题描述】:

我有一个 PHP 脚本来制作一个包含 2 个函数的 zip:

  1. dirToArray : 获取数组中的所有文件/empty_folders
  2. create_zip : 调用 dirToArray() 并制作 zipArchive

我收到了一个奇怪的警告,实际上是一个真正的错误,因为我的 zip 存档没有构建。

警告结果

警告:ZipArchive::close():读取错误:第 x 行的 path/to/file.php 中的文件描述符错误

谁能解释一下:“错误的文件描述符”是什么意思?

这是代码:

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;
    }
}

【问题讨论】:

  • 我的猜测是,调用open() 失败,然后您尝试关闭描述符,因为它尚未打开,所以不存在
  • 我检查了: if( $opened !== true ){ $GLOBALS["errors"][] = "Impossible to open {$zipProductPath} to edit it."; }
  • 是的,但它不会停止脚本的执行
  • 我做了测试来检查这些情况,我添加了 return false;... zip 已打开。

标签: php ziparchive


【解决方案1】:

我发现了问题...我对 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;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-17
    • 2011-09-25
    • 2012-02-20
    相关资源
    最近更新 更多