0

So I found this class on the internet that can put files into a zip. I have a form where I can upload pictures into a folder, then I tried to loop into these files and store them into a zip file but seems I failed somewhere.

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");

the last code is inside a foreach that loops into the files uploaded.

I don't know why I fail in creating the archive at the end ...

Could be an easy way to turn these new created folders into a zip or add them into a zip ?

Bououm
  • 23
  • 1
  • 8
  • 1
    Possible duplicate of [How to zip a whole folder using PHP](http://stackoverflow.com/questions/4914750/how-to-zip-a-whole-folder-using-php) – paskl Apr 15 '16 at 03:34
  • I may be wrong and am not in a position to test atm but if i am not mistaken you need to jump through some hoops for file_exists() to work with full url. Try just passing it the relative location of the file without BASE_URL. Also throw in some debug lines and tell us more e.i. `if(!file_exists($file)){ unset($this->_files[$index]); print_r("unset" . $file); } and foreach ($this->_files as $file) { $this->_zip->addFile($file, $file); print_r("Added file:" . $file); }` – Thomas B Apr 15 '16 at 03:37
  • @ThomasB I get nothing from `print_r("unset" . $file)` means no files have been deleted, and from the `print_r("Added file:" . $file);` I get the 3 files have been added. I did change the BASE_URL and it worked, but the problem is, after opening the archive I only find the last file uploaded even $file says that has added all but actually only added the last file. – Bououm Apr 15 '16 at 03:53
  • Added file:../upload/path/7 001.jpg Added file:../upload/path/8 001.jpg Added file:../upload/path/9 001.jpg @ThomasB – Bououm Apr 15 '16 at 03:58

1 Answers1

0

First problem was because I added BASE_URL I had to write ../upload/. Second problem was kinda stupid ... I had to create the class outside the foreach $zipper = new zipper; and $zipper-store("../upload/" . $uniqUser . ".zip");for unnecessary repeating (added it at the bottom).

Bououm
  • 23
  • 1
  • 8