无法添加 zip 中的所有文件 (php)

Can't add all files in the zip (php)

所以我在互联网上找到了这个 class 可以将文件放入 zip 中。 我有一个可以将图片上传到文件夹中的表单,但是当我尝试将它们添加到 zip 中时,我只能添加该文件夹中的最后一个文件(循环)。

zipper class

<?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();
                }
            }

        }
}

and this is my uploading file

> $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 中,循环到上传的文件中。

当我执行 print_r("Added file:" . $file . "<br>"); 以查看是否添加了所有文件时,我得到了肯定的响应。但我总是从文件夹中获取最后一个文件。 谢谢

EDIT Well, Guess the problem is after each loop to add the files in the folder, the zip class adds the file into the zip, then after checks if the zip exists or no ... So after each loop the zip is overwritten and adds only the last file. How can I make this better ?

这个问题有点愚蠢... 我必须在 foreach $zipper = new zipper; 和 $zipper-store("../upload/" . $uniqUser . ".zip"); 之外创建 class 用于不必要的重复(将其添加到底部)。