Codeigniter Zip库

时间:2015-06-26 12:49:03

标签: php codeigniter zip

您好我只是想知道是否可以使用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,但由于一些奇怪的原因,它忽略了一些文件。

1 个答案:

答案 0 :(得分:0)

示例 - 我的拉链

/**
 * 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(源文件,存档名称);