【问题标题】:While downloading a zip file from server my PHP website get stucked从服务器下载 zip 文件时,我的 PHP 网站卡住了
【发布时间】:2013-11-29 05:26:32
【问题描述】:

我制作了一个脚本,可以压缩服务器上的某些文件并下载,但问题是在下载 zip 时我无法在网站上的任何位置导航。它会卡住,直到下载未完成或取消。我正在使用以下代码制作 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();
    }

?>

【问题讨论】:

  • 你可以试试 if (ob_get_level()) {ob_end_clean();} readfile($pp111);看看你得到了什么?
  • 不工作仍然同样的问题无法在下载 zip 时在网站上导航。

标签: php zip download


【解决方案1】:

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

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

【讨论】:

  • 我尝试了与您在此处解释的相同的方法,但它不起作用,但有任何方法谢谢您的帮助。
猜你喜欢
  • 2018-12-26
  • 2018-07-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 2019-12-12
  • 1970-01-01
相关资源
最近更新 更多