How to create a zip folder from the database

Here is my code.Please anyone help me please

public function actionDownload(){
        $emp_id = Yii::$app->request->get('papl_id');
        $temp_id = str_replace('PAPL', 'TEMP', $emp_id);        
        $enrollment_table = Enrollment::find()->select(['browse_adhar','browse_pp_photo','browse_experience','esic_sheet','uan_sheet'])->where(['enrolement_id'=>$temp_id])->one();
        $family_table = Family::find()->select(['family_nominee_adhar_photo'])->where(['enrolement_id'=>$temp_id])->one();
        $document_table = Document::find()->select(['voter_copy_photo','drivinglicense_photo','pan_photo','passport_photo'])->where(['enrolement_id'=>$temp_id])->one();
        $nominee_table = Nominee::find()->select(['nominee_adhar_photo'])->where(['enrolement_id'=>$temp_id])->one();
        // echo "<pre>";print_r($document_table);die();
        // echo Yii::getAlias('@storage').$enrollment_table->browse_adhar;die();
        // echo '<pre>';print_r($document_table);die();
        $doc_arr = [];
        
        if($document_table){
            $doc_arr[]=$document_table->drivinglicense_photo;
            $doc_arr[]=$document_table->voter_copy_photo;
            $doc_arr[]=$document_table->pan_photo;
            $doc_arr[]=$document_table->passport_photo;
        }
        if ($enrollment_table) {
            $doc_arr[]=$enrollment_table->browse_adhar;
            $doc_arr[]=$enrollment_table->browse_pp_photo;
            $doc_arr[]=$enrollment_table->browse_experience;
            $doc_arr[]=$enrollment_table->esic_sheet;
            $doc_arr[]=$enrollment_table->uan_sheet;
        }
        if ($family_table) {
            $doc_arr[]=$family_table->family_nominee_adhar_photo;
        }
        if ($nominee_table) {
            $doc_arr[]=$nominee_table->nominee_adhar_photo;
        }
         
        // $doc_arr[] = array($enrollment_table,$family_table,$document_table,$nominee_table);
       $destination= Yii::getAlias('@storage');
            $zip=new ZipArchive();
      
              $zipname='document.zip';
             // $tmp_file = tempnam($destination.'archive/','');
        $zip->open($zipname, ZipArchive::CREATE);
        
        
        // echo "<pre>";print_r($destination);
        foreach($doc_arr as $thefile)
        {
       
            $full_file = $destination.$thefile;
            // echo $full_file;die();
            // $download_file = file_get_contents($full_file);
            if (is_file($full_file)) {
          
             $zip->addFile($full_file);
            // $zip->addFromString('test.txt', $full_file);
            
        }else{
           echo "error";  
        }
            
          
        }
       
        $zip->close();
       
        header('Content-Type: application/zip');
      header('Content-disposition: attachment; filename='.$zipname);
      header('Content-Length: ' . filesize($zipname));
      readfile($zipname);
        // var_dump($status);die();
        
    }

What is the question?

actually it is not creating zip folder if files available it creates zip folder but invalid zipfoder…

This site can’t be reached

The webpage at http://165.232.181.242/papl/backend/posting-history/download?papl_id=PAPL000053 might be temporarily down or it may have moved permanently to a new web address.

ERR_INVALID_RESPONSE

This message is showing

can you solve the issue it’s urgent.

Have you enabled DEBUG inside web/index.php?

Have you checked web server log?

Because this errore Is too generic.

is my code correct?? zip folder is downloading but while opening the zip folder it says it is invalid.

Hi @kalyani1990,

What do you mean by “invalid”? I don’t understand clearly what you want to ask.

  1. Is your “document.zip” file created as you intended or not? Does it have all the necessary files or not?
  2. Or do you fail to download the created zip file via user browser?

Or, it’s just a guess, but adding a line at the end of the code may solve the problem:

    ...
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename='.$zipname);
    header('Content-Length: ' . filesize($zipname));
    readfile($zipname);
    exit;
}

no document file is not created as per the requirement.i dont know whether the files are storing or not inside zip folder

files are not storing inside the zipfolder it is downloading a invalid zipfolder. means while extracting the zipfolder it shows error message invalid zip folder.

Screenshot 2021-09-02 181948

this error message is showing while extracting the zip folder

Can you download the zip file created on the server side via FTP and check if it is a valid zip file?

i didnot understand your question. can you provide me code for how to store files inside a zipfolder and automatically download it when we click on download button .

I’m sorry I can’t provide a quick solution.

I would check if the zip file is created correctly on the server side or not. If it is created correctly, then the problem is in the code that lets user download the file (header() and so on).

If you feel urgent, you have to check it ASAP.

I made a test code like the following, and it worked as expected.

    /**
     * Downloads photos
     * @param $id integer Staff ID
     */
    public function actionDownload($id)
    {
        $staff = Staff::findOne($id);
        // zip file name
        $zipFileName = "staff-" . $staff->staff_no . ".zip";
        // temporary directory
        $tempDir = sys_get_temp_dir();
        // zip file name in full path
        $zipFilePath = $tempDir . DIRECTORY_SEPARATOR . $zipFileName;
        
        $zip =  new ZipArchive();
        if (!$zip->open($zipFilePath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
            throw new UserException("Failed to create a zip archive. zipFilePath = $zipFilePath");
        }
        
        // file storage directory
        $storage = Yii::getAlias('@app') . DIRECTORY_SEPARATOR . 'photos';
        // add phptos to the zip archive
        $zip->addFile($storage . DIRECTORY_SEPARATOR . $staff->photoA, $staff->photoA);
        $zip->addFile($storage . DIRECTORY_SEPARATOR . $staff->photoB, $staff->photoB);
        $zip->addFile($storage . DIRECTORY_SEPARATOR . $staff->photoC, $staff->photoC);
        $zip->close();

        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename=' . $zipFileName);
        header('Content-Length: ' . filesize($zipFilePath));
        readfile($zipFilePath);

        unlink($zipFilePath);

        exit;
    }
  • required “ext-zip” in composer-json
  • used “\ZipArchive”
  • zip file will be created in the system temporary directory (and will be removed after the download).
  • made a distinction between the file name and the file path
  • explicitly specified the 2nd parameter of “addFile” with the file name.
  • exited before returning from the method, because manually sending headers and contents within the controller action is not what Yii expects from us.

Hope it will be a help to you.

Thank you very much. I will test in my code.