Zip-библиотека Codeigniter

Привет, я просто хотел узнать, можно ли использовать библиотеку codeigniter Zip для создания zip-файла на локальном диске, а затем добавлять в него файлы?

Ниже приведен код

$this->load->library('zip');
if (($handle = fopen($media_file, "r")) !== FALSE) 
        {
            $row = 1;

            while (($data = fgetcsv($handle)) !== FALSE) 
            {
                if($row > 1)
                {
                    $image_name = isset($data[1]) ? $data[1] : null;

                    if(!empty($image_name))
                    {
                        $this->zip->read_file("C:/files/Images/$image_name"); 
                    }   
                }

                $row++;
            }
        }
         $this->zip->download('Media.zip');

Вместо загрузки zip я хотел бы создать zip на локальном диске и просто добавить в него файлы.

Я был бы очень признателен, если бы кто-нибудь помог мне. Я пытался использовать php zip, но по какой-то странной причине он игнорирует некоторые файлы.


person user4676307    schedule 26.06.2015    source источник


Ответы (1)


пример - моя молния

/**
 * Created by websky
 */
class myZipper extends ZipArchive{
    protected $dir;
    protected $archive;
    protected $pathsArray;

    /**
     * @param string $dir
     * @param string $name
     */
    public function __construct($dir,$name){
        $this->dir = $dir;
        $this->archive = $name;
        $this->open($this->archive, myZipper::CREATE);
        $this->myScanDir($this->dir);
        $this->addZip();
        $this->getZip();
    }

    /**
     * @param string $dir
     */
    protected function myScanDir($dir){
        $files = scandir($dir);
        unset($files[0], $files[1]);
        foreach ($files as $file) {
            if(is_dir($dir.'/'.$file)){
                $this->myScanDir($dir.'/'.$file);
            }
        else {
                $this->pathsArray[] = array('oldpath' => $dir.'/'.$file, 'newpath'=>  (($this->dir == $dir)? $file : str_replace($this->dir.'/', '', $dir).'/'.$file));
            }
        }
    }

    protected function addZip(){
        foreach($this->pathsArray as $path){
            $this->addFile($path['oldpath'],$path['newpath']);
        }
    }

    public function getZip(){
        $this->close();
        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename='.$this->archive);
        header('Content-Length: '.filesize($this->archive));
        readfile($this->archive);
        unlink($this->archive);
    }
    }

    $test = new myZipper('C:/files/Images', 'Images.zip');

myZipper(исходные файлы, имя архива);

person websky    schedule 26.06.2015