从服务器下载zip文件时,我的PHP网站卡住了

时间:2013-11-29 05:26:33

标签: php zip download

我制作了一个脚本,可以在服务器上下载某些文件并下载,但问题是在zip下载时我无法浏览网站上的任何位置。在下载未完成或取消之前,它会被卡住。我使用下面的代码制作一个拉链并下载

<?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();
    }

?>

1 个答案:

答案 0 :(得分:1)

如果您不关闭会话,会话文件将对来自同一用户的所有其他请求保持锁定状态。如果下载需要60秒,则用户必须等待60秒。

您应该在脚本之上获取所需的所有数据,然后close the session以释放文件。