简体   繁体   中英

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. 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.

I would suggest you to first check you content availability and then create zip file and at last download it.

To create ZIP file

$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'];
       }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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