Create a zip file and download it

后端 未结 7 705
滥情空心
滥情空心 2020-11-29 04:16

I am trying to download a 2 files by creating the zip file on local-server.the file is downloaded in zip format but when i try to extract it.it gives error: End-of-

相关标签:
7条回答
  • 2020-11-29 04:59

    but the file i am getting from server after download it gives the size of 226 bytes

    This is the size of a ZIP header. Apparently there is no data in the downloaded ZIP file. So, can you verify that the files to be added into the ZIP file are, indeed, there (relative to the path of the download PHP script)?

    Consider adding a check on addFile too:

    foreach($file_names as $file)
    {
        $inputFile = $file_path . $file;
        if (!file_exists($inputFile))
            trigger_error("The input file $inputFile does not exist", E_USER_ERROR);
        if (!is_readable($inputFile))
            trigger_error("The input file $inputFile exists, but has wrong permissions or ownership", E_USER_ERROR);
        if (!$zip->addFile($inputFile, $file))
            trigger_error("Could not add $inputFile to ZIP file", E_USER_ERROR);
    }
    

    The observed behaviour is consistent with some problem (path error, permission problems, ...) preventing the files from being added to the ZIP file. On receiving an "empty" ZIP file, the client issues an error referring to the ZIP central directory missing (the actual error being that there is no directory, and no files).

    0 讨论(0)
  • 2020-11-29 05:00
    $zip = new ZipArchive;
    $tmp_file = 'assets/myzip.zip';
        if ($zip->open($tmp_file,  ZipArchive::CREATE)) {
            $zip->addFile('folder/bootstrap.js', 'bootstrap.js');
            $zip->addFile('folder/bootstrap.min.js', 'bootstrap.min.js');
            $zip->close();
            echo 'Archive created!';
            header('Content-disposition: attachment; filename=files.zip');
            header('Content-type: application/zip');
            readfile($tmp_file);
       } else {
           echo 'Failed!';
       }
    
    0 讨论(0)
  • 2020-11-29 05:01
    // http headers for zip downloads
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"".$filename."\"");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($filepath.$filename));
    ob_end_flush();
    @readfile($filepath.$filename);
    

    I found this soludtion here and it work for me

    0 讨论(0)
  • 2020-11-29 05:02

    I just ran into this problem. For me the issue was with:

    readfile("$archive_file_name");
    

    It was resulting in a out of memory error.

    Allowed memory size of 134217728 bytes exhausted (tried to allocate 292982784 bytes)
    

    I was able to correct the problem by replacing readfile() with the following:

        $handle = fopen($zipPath, "rb");
        while (!feof($handle)){
            echo fread($handle, 8192);
        }
        fclose($handle);
    

    Not sure if this is your same issue or not seeing that your file is only 1.2 MB. Maybe this will help someone else with a similar problem.

    0 讨论(0)
  • 2020-11-29 05:06

    I have experienced exactly the same problem. In my case, the source of it was the permissions of the folder in which I wanted to create the zip file that were all set to read only. I changed it to read and write and it worked.

    If the file is not created on your local-server when you run the script, you most probably have the same problem as I did.

    0 讨论(0)
  • 2020-11-29 05:07

    Add Content-length header describing size of zip file in bytes.

    header("Content-type: application/zip"); 
    header("Content-Disposition: attachment; filename=$archive_file_name");
    header("Content-length: " . filesize($archive_file_name));
    header("Pragma: no-cache"); 
    header("Expires: 0"); 
    readfile("$archive_file_name");
    

    Also make sure that there is absolutely no white space before <? and after ?>. I see a space here:

     <?php
    $file_names = array('iMUST Operating Manual V1.3a.pdf','iMUST Product Information Sheet.pdf');
    
    0 讨论(0)
提交回复
热议问题