【问题标题】:Can't create zip file (php)无法创建 zip 文件 (php)
【发布时间】:2016-04-15 02:34:35
【问题描述】:

所以我在互联网上找到了这个可以将文件放入 zip 的类。 我有一个表单,可以将图片上传到文件夹中,然后我尝试循环进入这些文件并将它们存储到一个 zip 文件中,但似乎我在某个地方失败了。

拉链类

<?php

class zipper {
    private $_files = array(),
            $_zip;

    public function __construct() {
        $this->_zip = new ZipArchive;
    }


    public function add($input){
        if(is_array($input)){
            $this->_files = array_merge($this->_files, $input);
        }else{
            $this->_files[] = $input;
        }

    }
        public function store($location = null){

            if(count($this->_files) && $location){
                foreach ($this->_files as $index => $file) {
                    if(!file_exists($file)){
                        unset($this->_files[$index]);
                    }
                    print_r($file . "<br>");// here gives me the exact path of the files.
                }

                if($this->_zip->open($location, file_exists($location) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)){
                    foreach ($this->_files as $file) {
                        $this->_zip->addFile($file, $file);
                    }

                    $this->_zip->close();
                }
            }

        }
}

这是我上传的文件

> $uniqUser = uniqid(strtoupper($userName). "-" , true); 
> $directory ='../upload/';  
> $file_name is $files['name']

if(!is_dir("../upload/". $uniqUser ."/")) {
                    mkdir("../upload/". $uniqUser ."/");
                }


                if(move_uploaded_file($file_tmp, $directory .  $uniqUser . "/" . $file_name)){
                    $uploaded[$position] = $directory . $uniqUser . "/" . $file_name;
                        $zipper = new zipper;
                        $zipper->add(BASE_URL . "/upload/" . $uniqUser . "/" . $file_name);
                        $zipper->store(BASE_URL . "/upload/" . $uniqUser . ".zip");

最后一个代码在一个 foreach 中,它循环到上传的文件中。

我不知道为什么我最后无法创建存档...

将这些新创建的文件夹转换为 zip 或将它们添加到 zip 是否是一种简单的方法?

【问题讨论】:

  • 我可能错了,我无法测试 atm,但如果我没记错的话,你需要跳过一些障碍才能让 file_exists() 使用完整的 url。尝试只传递不带 BASE_URL 的文件的相对位置。还添加一些调试行并告诉我们更多 e.i. if(!file_exists($file)){ unset($this-&gt;_files[$index]); print_r("unset" . $file); } and foreach ($this-&gt;_files as $file) { $this-&gt;_zip-&gt;addFile($file, $file); print_r("Added file:" . $file); }
  • @ThomasB 我没有从print_r("unset" . $file) 得到任何信息,这意味着没有文件被删除,而从print_r("Added file:" . $file); 我得到的3 个文件已被添加。我确实更改了 BASE_URL 并且它有效,但问题是,在打开存档后,我只找到最后一个上传的文件,即使 $file 说已经添加了所有但实际上只添加了最后一个文件。
  • 添加文件:../upload/path/7 001.jpg 添加文件:../upload/path/8 001.jpg 添加文件:../upload/path/9 001.jpg @ThomasB

标签: php zip archive


【解决方案1】:

第一个问题是因为我添加了BASE_URL,所以我必须写../upload/。 第二个问题有点愚蠢... 我必须在 foreach $zipper = new zipper; 和 $zipper-store("../upload/" . $uniqUser . ".zip"); 之外创建类以进行不必要的重复(在底部添加)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多