对于提出这个问题的人来说可能为时已晚,但是可以在脚本结束之前使用函数 session_write_close() 解锁会话。你可以这样做:
<?php
session_start();
// ... use and/or update session data
session_write_close();
// begin streaming
header("Content-Type: foo/bar");
$f=fopen("hundred-meg-video.mp4","rb");
fpassthru($f);
fclose($f);
?>
或者,您可以在流结束时重新打开会话。我们使用它来限制每个用户的并发下载数量:
<?php
$limit=4;
session_start();
if ($_SESSION["concurrent_downloads"]>$limit) {
header("HTTP/1.1 500 Internal Server Error");
echo "too many concurrent downloads";
die;
}
ignore_user_abort(1); // if the user aborts the script, we still want it to continue running on the server
// otherwise the $_SESSION["concurrent_downloads"] would not decrease at its end
$_SESSION["concurrent_downloads"]++;
session_write_close();
// begin streaming
header("Content-Type: foo/bar");
$f=fopen("hundred-meg-video.mp4","rb");
fpassthru($f);
fclose($f);
// resume using session
@session_start(); // @ used to suppress "headers already sent" message
$_SESSION["concurrent_downloads"]--;
?>