ZIP文件夹并在代码点火器中下载


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格式未知或损坏。 请帮助 我可以下载文件并将其压缩吗 这是在代码点火器中。 谢谢大家

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

}

这是我问题的解决方案

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

最新更新