繁体   English   中英

来自文件夹的PHP Zip文件-没有错误-没有存档

[英]PHP Zip Files from an Folder - no error - no archive

我对PHP 5.2x和“ ZipArchive”感到疯狂,下面的代码不会引发错误,每个部分都返回true,但是没有创建ZIP文件。

有人可以看到我看不到的错误吗?

if ($handle = opendir($directory)) {
        $zip = new ZipArchive();

        $filename = time() . '.zip';
        $path = APPLICATION_PATH . '/../download/';

        $status = $zip->open($path . $filename, ZIPARCHIVE::CREATE);

        if ($status !== true) {
            $this->log->err('Cant create zipArchive!');
            return false;
        } 

        while (false !== ($file = readdir($handle))) {
            if (!is_dir($file)) {
                if (is_readable($file)) {
                    if (!$zip->addFile($file)) {
                        $this->log->err('Cant add File to archive: ' . $file);
                    }
                } else {
                     $this->log->debug('Added file to archive: ' . $file);
                }
            }
        }

        closedir($handle);

        if (!$zip->close()) {
            $error = $zip->getStatusString();
            $this->log->err($error); 
            $this->log->err('Cant create archive..');
        } 
    }

您实际上从未在ZIP存档中添加任何文件。 这是您没有错误的唯一原因。

is_readable($file)始终返回false ,当它执行操作时,将显示“ Added file to archive ... ,这可能是因为您放错了else块。

您必须将文件夹添加到$file ,在这种情况下,它应该是$directory

while (false !== ($file = readdir($handle))) {
    if (!is_dir($directory . $file)) {
        if (is_readable($directory . $file)) {
           if (!$zip->addFile($directory . $file)) {
                $this->log->err('Cant add File to archive: ' . $file);
            } else {
                $this->log->debug('Added file to archive: ' . $file);
            }
        } else {
            $this->log->debug('File not readable: ' . $file);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM