Opening downloaded zip file creates cpgz file?

后端 未结 6 1841
盖世英雄少女心
盖世英雄少女心 2020-12-10 06:25

If I make the url for a zip file the href of a link and click the link, my zip file gets downloaded and opening it gets the contents as I expect.

Here\'

6条回答
  •  孤城傲影
    2020-12-10 06:57

    The PHP documentation for readfile says that it will output the contents of a file and return an int.

    So your code, echo readfile("$archive");, will echo $archive (btw, the double quotes are meaningless here; you should remove them), and THEN output the int that is being returned. That is, your line should be: readfile($archive);

    Also, you should be using a local path (not an http:// link) to the archive.

    Altogether:

    if($yesCanDownload){
        $archive='/path/to/my-archive.zip';
        header("Content-Type: application/zip");
        header("Content-Disposition: attachment; filename=".basename($archive));
        header("Content-Length: ".filesize($archive));
        ob_clean();
        flush();
        readfile($archive);
    }
    

    Lastly, if that does not work, make sure filesize($archive) is returning the accurate length of the file.

提交回复
热议问题