zip文件夹并在codeigniter中下载

时间:2018-06-04 06:58:15

标签: php codeigniter codeigniter-2 codeigniter-3 codeigniter-datamapper

public function folderdownload(){
    try{
            $this->load->library('zip');    
            $this->load->helper('file');
            $where =  array(
            'file_perm_id'=>$this->input->post('id'));
            $this->load->model('fetch_model');
            $file_path = $this->fetch_model->getalldata($this->folderpath,$this->master,$where);
            $path = @@$file_path[0]->folder_path ;
            $files = get_filenames($path);
           // when i used print_r($files); to verify that i can see the files i can see it from here 
            foreach($files as $f){
                 if (is_file($path . $f)) 
                $this->zip->add_data($f, file_get_contents($path . $f));

            }
            ob_end_clean();
            $this->zip->download(date('m-d-Y'));
    }catch(Exception $e){
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }



        }

我有这个控制器,一旦使用点击下载按钮,它下载文件夹中的所有文件,但是当我打开它时,它表示archieve是未知格式或损坏。请帮助hoiw我可以下载文件并压缩它这是在codeigniter。谢谢任何人

2 个答案:

答案 0 :(得分:0)

public function folderdownload(){

    try{
            $this->load->library('zip');    
            $this->load->helper('file');
            $where =  array(
            'file_perm_id'=>$this->input->get('id'));
            $this->load->model('fetch_model');
            $file_path = $this->fetch_model->getalldata($this->folderpath,$this->master,$where);
            $path =$file_path[0]->folder_path ;
            $finallink = ($_SERVER['DOCUMENT_ROOT'] . "/cobacfms/" . $path);
            $this->zip->read_dir(($finallink), false);
            $this->zip->download(date('m-d-Y'));
    }catch(Exception $e){
        echo 'Caught exception: ',  $e->getMessage(), "\n";
    }



        }

这是我的问题的解决方案

答案 1 :(得分:-1)

您的zip文件创建存在问题。

我建议您首先查看内容可用性,然后创建zip文件,最后下载。

创建ZIP文件

$sourcePath = 'uploads/sourceDirectory';
$targetPath = 'uploads/destDirectory';    
if (file_exists($sourcePath)){
        if(!copy($sourcePath, $targetPath)){ //Copy file source to destination directory
              return ['status' => false, 'msg' => 'File missing'];  
          }
}

if (file_exists($targetPath)){ // check target directory
       $this->zip->read_dir($targetPath,False); // read target directory
       if(!$this->zip->archive($targetPath.'.zip')){ // zip target directory
            return ['status' => false, 'msg' => 'Zip file creation Failed!'];
       }else{
            return ['status' => false, 'msg' => 'Zip file Created'];
       }
}