Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php创建和下载zip文件_Php_Codeigniter_Zip_Download - Fatal编程技术网

使用php创建和下载zip文件

使用php创建和下载zip文件,php,codeigniter,zip,download,Php,Codeigniter,Zip,Download,我正在尝试为此创建一个zip文件(使用php),我编写了以下代码: $fileName = "1.docx,2.docx"; $fileNames = explode(',', $fileName); $zipName = 'download_resume.zip'; $resumePath = asset_url() . "uploads/resume/"; //http://localhost/mywebsite/public/uploads/resume/ $zip = new ZipA

我正在尝试为此创建一个zip文件(使用php),我编写了以下代码:

$fileName = "1.docx,2.docx";
$fileNames = explode(',', $fileName);
$zipName = 'download_resume.zip';
$resumePath = asset_url() . "uploads/resume/";
//http://localhost/mywebsite/public/uploads/resume/

$zip = new ZipArchive();
if ($zip->open($zipName, ZIPARCHIVE::CREATE) !== TRUE) {
    echo json_encode("Cannot Open");
}

foreach ($fileNames as $files) {
    $zip->addFile($resumePath . $files, $files);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zipName."");
header("Content-length: " . filesize($zipName));
header("Pragma: no-cache"); 
header("Expires: 0");
readfile($zipName);
exit;
然而,点击一个按钮,我没有得到任何东西,甚至没有任何错误或消息

任何帮助或建议都会对我大有裨益。。提前谢谢

  • 什么是
    asset\u url()
    函数?尝试使用
    APPPATH
    constant istead此函数:

    $resumePath = APPPATH."../uploads/resume/";
  • 另外,我认为使用CI-zip库是更好的选择。简单的例子:

    public function generate_zip($files = array(), $path)
    {
        if (empty($files)) {
            throw new Exception('Archive should\'t be empty');
        }
        $this->load->library('zip');
        foreach ($files as $file) {
            $this->zip->read_file($file);
        }
        $this->zip->archive($path);
    }
    
    public function download_zip($path)
    {
        if (!file_exists($path)) {
            throw new Exception('Archive doesn\'t exists');
        }
        $this->load->library('zip');
        $this->zip->download($path);
    }
    

    为什么不在Codeigniter中使用Zip编码类呢?它会帮你做到这一点

    $name = 'mydata1.txt';
    $data = 'A Data String!';
    
    $this->zip->add_data($name, $data);
    
    // Write the zip file to a folder on your server. Name it "my_backup.zip"
    $this->zip->archive('/path/to/directory/my_backup.zip'); 
    
    // Download the file to your desktop. Name it "my_backup.zip"
    $this->zip->download('my_backup.zip');
    

    下面的脚本在我的本地系统中工作正常。首先从$resumePath中删除资产_url(),并设置zip文件存储位置相对路径。 -将zip文件名及其位置路径传递到$zip->open()

    。。。这对我有用

    public function downloadall(){
    
            $createdzipname = 'myzipfilename';
    
            $this->load->library('zip');
            $this->load->helper('download');
            $cours_id = $this->input->post('todownloadall');
            $files = $this->model_travaux->getByID($cours_id); 
    
            // create new folder 
            $this->zip->add_dir('zipfolder');
    
            foreach ($files as $file) {
                $paths = 'http://localhost/uploads/'.$file->file_name.'.docx';
                // add data own data into the folder created
                $this->zip->add_data('zipfolder/'.$paths,file_get_contents($paths));    
            }
            $this->zip->download($createdzipname.'.zip');
        }
    

    /*创建压缩文件夹*/

    public function zip(){
        $getImage = $this->cart_model->getImage();
        $zip = new ZipArchive;
        $auto = rand();
        $file = date("dmYhis",strtotime("Y:m:d H:i:s")).$auto.'.zip';
        if ($zip->open('./download/'.$file,  ZipArchive::CREATE)) {
            foreach($getImage as $getImages){
                $zip->addFile('./assets/upload/photos/'.$getImages->image, $getImages->image);
            }
            $zip->close();
            $downloadFile = $file;
            $download = Header("Location:http://localhost/projectname/download/".$downloadFile);
    
    
        }
    }
    
    模型------

    /*获取添加到购物车图像*/

    public function getImage(){
        $user_id = $this->session->userdata('user_id');
        $this->db->select('tbl_cart.photo_id, tbl_album_image.image as image');
        $this->db->from('tbl_cart');
        $this->db->join('tbl_album_image', 'tbl_album_image.id = tbl_cart.photo_id', 'LEFT');
        $this->db->where('user_id', $user_id);
        return $this->db->get()->result();
    }
    

    您应该接受此配偶的回答:)
    public function downloadall(){
    
            $createdzipname = 'myzipfilename';
    
            $this->load->library('zip');
            $this->load->helper('download');
            $cours_id = $this->input->post('todownloadall');
            $files = $this->model_travaux->getByID($cours_id); 
    
            // create new folder 
            $this->zip->add_dir('zipfolder');
    
            foreach ($files as $file) {
                $paths = 'http://localhost/uploads/'.$file->file_name.'.docx';
                // add data own data into the folder created
                $this->zip->add_data('zipfolder/'.$paths,file_get_contents($paths));    
            }
            $this->zip->download($createdzipname.'.zip');
        }
    
    public function zip(){
        $getImage = $this->cart_model->getImage();
        $zip = new ZipArchive;
        $auto = rand();
        $file = date("dmYhis",strtotime("Y:m:d H:i:s")).$auto.'.zip';
        if ($zip->open('./download/'.$file,  ZipArchive::CREATE)) {
            foreach($getImage as $getImages){
                $zip->addFile('./assets/upload/photos/'.$getImages->image, $getImages->image);
            }
            $zip->close();
            $downloadFile = $file;
            $download = Header("Location:http://localhost/projectname/download/".$downloadFile);
    
    
        }
    }
    
    public function getImage(){
        $user_id = $this->session->userdata('user_id');
        $this->db->select('tbl_cart.photo_id, tbl_album_image.image as image');
        $this->db->from('tbl_cart');
        $this->db->join('tbl_album_image', 'tbl_album_image.id = tbl_cart.photo_id', 'LEFT');
        $this->db->where('user_id', $user_id);
        return $this->db->get()->result();
    }