简体   繁体   English

zip文件夹并在codeigniter中下载

[英]zip folder and download in codeigniter

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



        }

I have this controller that once the use click the download button it download all the files within the folder but when I open it it says that the archieve is either unknown format or damaged. 我有这个控制器,一旦使用它就单击下载按钮,它会下载文件夹中的所有文件,但是当我打开它时,它表示档案格式不明或已损坏。 please help hoiw can I download files and zip it this is in codeigniter. 请帮助hoiw我可以下载文件并压缩它吗,这在codeigniter中。 thanks anyone 谢谢任何人

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



        }

This is the solution to my problem 这是我的问题的解决方案

Your zip file creation is having issue. 您的zip文件创建出现问题。

I would suggest you to first check you content availability and then create zip file and at last download it. 我建议您首先检查内容的可用性,然后创建zip文件,最后下载它。

To create ZIP file 创建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'];
       }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM