简体   繁体   中英

While downloading a zip file from server my PHP website get stucked

I have made a script which zips certain files on server and downloads but issue is while zip is downloading i can't navigate any where on the site. it get stucked until download is not finished or cancelled. i am using below code for making a zip and downloading it

<?php
     $files = $_SESSION['cart']['all'];

    function createZip($files, $zip_file) {
        $zip = new ZipArchive;

        if ($zip->open($zip_file, ZipArchive::OVERWRITE) === TRUE) {
            foreach ($files as $file) {

                if ($file->songType == "1") {
                    $zip->addFile('assets/songs/' . $file->filePath, $file->filePath);
                } else if ($file->songType == "2") {
                    $zip->addFile('assets/videos/' . $file->filePath, $file->filePath);
                }
            }
            $zip->close();

            $_SESSION['cart'] = array();
            $_SESSION['cart']['temp'] = array();
            $_SESSION['cart']['all'] = array();
            return true;
        }
        else
            return false;
    }

    $temp = 'file.zip';
    $pp111 = $temp;

    if (createZip($files, $pp111)) {
        //  header('Content-Type: application/octet-stream');
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");  //Update modified time to current time.
        header("Content-Length: " . filesize($pp111)); //file size
        header("Content-Disposition: attachment; filename=\"" . stripslashes($pp111) . "\""); //give the file a name.

        ob_clean();
        flush();
        readfile($pp111); // now start reading the file on your server to start downloading to user's desktop */
        // unlink($pp111);
        ob_end_flush();
        ob_end_clean();
        exit();
    } else {
        exit();
    }

?>

If you don't close the session, the session file remains locked for all other requests from the same user. If download takes 60 seconds, user has to wait 60 seconds.

You should grab all the data you need on top of the script and then close the session to release the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM