ZipArchive :: close():读取错误:是目录 [英] ZipArchive::close(): Read error: Is a directory

查看:1436
本文介绍了ZipArchive :: close():读取错误:是目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图找出这个问题,但我无法想象为什么它一直在发生。我正在将文件添加到ZipArchive,当我尝试将其关闭时,会收到以下错误消息:目标位置是目录。

I'm trying to figure out this problem but I cannot imagine why it keeps happening. I'm adding files to a ZipArchive and when I try to close it, it get the error that the destination is a directory. But I'm pretty sure it is not.

这是zip函数的代码:

This is the code of the zip function:

function create_zip($folder, $destination) {
    $valid_files = get_files($folder);
    if(count($valid_files)) {       
        $zip = new ZipArchive();
        if($zip->open($destination, ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        //add the files
        foreach($valid_files as $file) {
            $zip->addFile($file,$file);
        }
        $zip->close();
        return file_exists($destination);
    }
    else
    {
        return false;
    }
}

function get_files($folder){
    $valid_files = array();
    $files = scandir($folder);
    foreach($files as $file) {
        if(substr($file, 0, 1) == "." || !is_readable($folder . '/' . $file)) {
            continue;
        }
        if(is_dir($file)){
            array_merge($valid_files, get_files($folder . '/' . $file));
        } else {
            $valid_files[] = $folder . '/' . $file;
        }
    }
    return $valid_files;
}

我这样称呼它,所以它实际上不应该是目录:

I'm calling it like this so it should really not be a directory:

$dest = "backups/" . time() . "_backup.zip";
if(file_exists($dest)){
    if(is_dir($dest)) {
        rmdir($dest);
    } else {
        unlink($dest);  
    }
}
create_zip('crawler/out', $dest);

也许有人可以帮我这个忙。谢谢!

Maybe someone here can help me with this. Thank you!

Simon

推荐答案

在这种情况下,目录不是zip存档,而是添加到其中的文件。

In this case, the directory is not zip archive, but the file that is added to it.

在添加文件之前尝试添加以下内容:

Try adding this before adding the file:

if (file_exists($file) && is_file($file))

并在此位置更改文件名而不是文件路径:

And change the filename instead of the filepath in this place:

$ zip-> addFile($ file, $ file );

$zip->addFile($file,$file);

这篇关于ZipArchive :: close():读取错误:是目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆