Unable to open zip file, codeigniter

2019-08-20 11:47发布

问题:

I tried to add my pictures, which uploaded by users, into a zip file. so, that user can download this file. The problem is when I download this zip file, it contains lots of filename (as I expect). But when I open this zip, it show me error “C:....ata\Local\Temp\my_photo.zip: Unexpected end of archive”

Each picture has size but CRC32 is 00000000 Here is the code:

 $this->load->library('zip');
 $this->zip->clear_data();
 foreach ($row as $ar){
    $file_relative_path = $ar->filepath;               
     if(file_exists($file_relative_path)){
           $this->zip->add_data($this->_getFileName($file_relative_path),file_get_contents($file_relative_path));
     }
}
$this->zip->download('my_photo.zip');  

回答1:

Try adding this right before the last line:

$this->zip->archive('my_photo.zip');

I found this in the documentation.

I'm guessing that this might work sometimes, but maybe headers are being sent in your case, so you need to use the archive method.