create and download zip file using php

前端 未结 5 747
借酒劲吻你
借酒劲吻你 2020-12-10 21:00

i am trying to create a zip file(using php) for this i have written the following code:

$fileName = \"1.docx,2.docx\";
$fileNames = explode(\',\', $fileName)         


        
相关标签:
5条回答
  • 2020-12-10 21:19

    /* create zip folder */

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

    model------

    /* get add to cart image */

    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();
    }
    
    0 讨论(0)
  • 2020-12-10 21:28
    1. What is asset_url() function? Try to use APPPATH constant istead this function:

      $resumePath = APPPATH."../uploads/resume/";
    2. Add "exists" validation for file names:

      foreach ($fileNames as $files) {
          if (is_file($resumePath . $files)) {
              $zip->addFile($resumePath . $files, $files);
          }
      }
    3. Add exit() after:

      echo json_encode("Cannot Open");

    Also I think it's the better desision to use CI zip library User Guide. Simple example:

    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);
    }
    
    0 讨论(0)
  • 2020-12-10 21:30

    ... it work for me

    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');
        }
    
    0 讨论(0)
  • 2020-12-10 21:31

    Below scripting working ok in my local system. 1st remove asset_url() from $resumePath and set zip file store location relative path. - Pass zip file name with its location path to $zip->open()

    $fileName = "1.docx,2.docx";
    $fileNames = explode(',', $fileName);
    $zipName   = 'download_resume.zip';
    $resumePath = "resume/";
    
    $zip = new ZipArchive();
    if ($zip->open($resumePath.$zipName, ZIPARCHIVE::CREATE) !== TRUE) {
     echo json_encode("Cannot Open");
    }
    
    foreach ($fileNames as $files) {
     $zip->addFile($files, $files);
    }
    $zip->close();
    
    0 讨论(0)
  • 2020-12-10 21:41

    Why not use the Zip Encoding Class in Codeigniter - it will do this for you

    $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');
    

    https://www.codeigniter.com/user_guide/libraries/zip.html

    0 讨论(0)
提交回复
热议问题