-1
<body>
    <?php
        $zip = new ZipArchive;

        if ($zip->open(getcwd() .'/read.zip', ZipArchive::CREATE) === TRUE) {
            $zip->addFile(getcwd() . '/read.txt','/newname.txt');
            $zip->close();


            $file = getcwd() . '/read.zip';
             // http headers for zip downloads
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header("Content-type: application/octet-stream");
            header("Content-Disposition: attachment; filename=\"read.zip\"");
            header("Content-Transfer-Encoding: binary");
            header("Content-Length: ".filesize($file));
            readfile($file);

            echo 'ok';

        } else {
            echo 'failed';
        }
    ?>
</body>

I have following code and want to download zip file automatically that is being produced by this code when we run this page.

Jay
  • 23
  • 8
  • Possible duplicate of [PHP automatically download file](https://stackoverflow.com/questions/10151417/php-automatically-download-file) – Erik Kalkoken Feb 06 '18 at 11:55

1 Answers1

0

u need to add header for the client about file downloading. just see the manual example: http://php.net/manual/en/function.header.php#example-5315

<?php
    $file = getcwd() . '/read.zip';
    // http headers for zip downloads
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"read.zip\"");
    header("Content-Transfer-Encoding: binary");
    header("Content-Length: ".filesize($file));
    ob_end_flush();
    @readfile($file);
myxaxa
  • 1,391
  • 1
  • 8
  • 7
  • I have tried the above code. but still its not working. I'm just a beginner. Can u please help me more? – Jay Feb 06 '18 at 12:20
  • I have added this headers after $zip->close(); – Jay Feb 06 '18 at 12:21
  • @Jay, sorry! I've just missed the main thing - readfile($file); – myxaxa Feb 06 '18 at 12:49
  • It generates o/p like........ PK�bFLOm:�/newname.txtHello I'm in reading mode.PK�bFLOm:���/newname.txtPK:Dok........ But not still downloading zip automatically – Jay Feb 07 '18 at 08:31
  • just a moment, I'll check it localy – myxaxa Feb 07 '18 at 08:54
  • okay I'm not getting any trouble in downloading text file by adding header().. But here I am getting strange o/p as I mentioned above. – Jay Feb 07 '18 at 10:28
  • is the zip file created? – myxaxa Feb 07 '18 at 11:01
  • well, remove the "echo" and try again – myxaxa Feb 08 '18 at 14:06
  • didn't you forget $zip->addFile(getcwd() . '/read.txt',getcwd() . '/newname.txt'); near the newname.txt? – myxaxa Feb 08 '18 at 14:19
  • Yeah I removed echo and tried but still its not working. Even I also add getcwd() before newname.txt but though its not working. It generates the output as I told u before like this..... PK�bFLOm:�3/Applications/MAMP/htdocs/Jay/php/exam1/newname.txtHello I'm in reading mode.PK�bFLOm:�3��/Applications/MAMP/htdocs/Jay/php/exam1/newname.txtPKak....... as I add getcwd() before newname.txt so it produced newname.txt code in the file as well but not downloading. – Jay Feb 09 '18 at 07:22
  • btw I'm using MAC but I don't think that its an OS problem. – Jay Feb 09 '18 at 07:23
  • there is an answer for your problem - https://stackoverflow.com/questions/12411481/php-download-script-creates-unreadable-zip-file-on-mac – myxaxa Feb 09 '18 at 08:00
  • ob_end_flush(); @readfile($file); i've editted the code above – myxaxa Feb 09 '18 at 08:00
  • its still not working.. I have tried all the answers which are in the link which you send. It becomes headache now. – Jay Feb 09 '18 at 10:44
  • hey, i didn't notice that you have html code: the body tag. remove it also – myxaxa Feb 09 '18 at 11:15
  • and remove the "echo". and update the main code at the top of the question pls – myxaxa Feb 09 '18 at 11:16
  • yeah! great! ^_^ – myxaxa Feb 09 '18 at 15:18