-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.

1

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

17 Comments

I have tried the above code. but still its not working. I'm just a beginner. Can u please help me more?
I have added this headers after $zip->close();
@Jay, sorry! I've just missed the main thing - readfile($file);
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
just a moment, I'll check it localy
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.