Php执行将停止,直到下载文件
我正在开发一个视频点播网站。下载选项随内容标题和readfile一起提供。我面临一个问题,我只能在前一个文件下载完成后才能查看或下载其他内容。 我当前的readfile代码Php执行将停止,直到下载文件,php,Php,我正在开发一个视频点播网站。下载选项随内容标题和readfile一起提供。我面临一个问题,我只能在前一个文件下载完成后才能查看或下载其他内容。 我当前的readfile代码 session_start(); header("Content-Type: application/force-download"); header("Content-Type: application/octet-stream"); header("Content-Type: application/download")
session_start();
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename='.$_SESSION['file']);
//set_time_limit(0);
readfile($_SESSION['file']);
exit();
可能有什么问题?问题在于会话:只要此请求正在运行,会话就会被锁定,因此您无法执行任何使用同一会话的操作 解决方案是将文件名的值分配给另一个变量,并在将内容输出到浏览器之前关闭会话 例如:
session_start();
$filename = $_SESSION['file'];
// close the session
session_write_close();
// output the data
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename='.$filename);
//set_time_limit(0);
readfile($filename);
exit();
问题在于会话:只要此请求正在运行,会话就会被锁定,因此您无法使用同一会话执行任何操作 解决方案是将文件名的值分配给另一个变量,并在将内容输出到浏览器之前关闭会话 例如:
session_start();
$filename = $_SESSION['file'];
// close the session
session_write_close();
// output the data
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename='.$filename);
//set_time_limit(0);
readfile($filename);
exit();
其他请求被阻止,因为会话文件被此文件锁定。 解决方案是在使用函数调用readfile之前关闭会话 像这样:
<?php
$file = $_SESSION['file'];
session_write_close();
readfile($file);
?>
其他请求被阻止,因为会话文件被此文件锁定。 解决方案是在使用函数调用readfile之前关闭会话 像这样:
<?php
$file = $_SESSION['file'];
session_write_close();
readfile($file);
?>
这是因为会话锁定。当您调用session_start时,PHP会锁定会话文件,以便在释放锁之前无法使用任何其他进程。这样做是为了防止并发写入 因此,当另一个请求到达服务器时,PHP会在session_开始行上等待,直到它能够使用session,也就是第一个请求结束时 您可以通过传递附加参数read_和_close以只读方式打开会话。如中所述
注:如手册所述,选项参数已添加到PHP7中。感谢蒙基宙斯指出这一点。如果您使用的是旧版本,可以根据jeroen的回答尝试关闭会话写入。这是因为会话锁定。当您调用session_start时,PHP会锁定会话文件,以便在释放锁之前无法使用任何其他进程。这样做是为了防止并发写入 因此,当另一个请求到达服务器时,PHP会在session_开始行上等待,直到它能够使用session,也就是第一个请求结束时 您可以通过传递附加参数read_和_close以只读方式打开会话。如中所述
注:如手册所述,选项参数已添加到PHP7中。感谢蒙基宙斯指出这一点。如果您使用的是旧版本,您可以根据jeroen的答案尝试session_write_close。由于其他答案都是仅适用于PHP的解决方案,因此我想提供PHP回退的惊人功能:
session_start();
// Set some headers
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename='.$_SESSION['file']);
if(in_array('mod_xsendfile', apache_get_modules()))
{
header("X-Sendfile: ".$_SESSION['file']);
}
else
{
$file = $_SESSION['file'];
// close the session
session_write_close();
//set_time_limit(0);
readfile($file);
}
exit(0);
由于其他答案都是仅适用于PHP的解决方案,我想提供一个PHP回退的惊人功能:
session_start();
// Set some headers
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header('Content-Type: video/mp4');
header('Content-Disposition: attachment; filename='.$_SESSION['file']);
if(in_array('mod_xsendfile', apache_get_modules()))
{
header("X-Sendfile: ".$_SESSION['file']);
}
else
{
$file = $_SESSION['file'];
// close the session
session_write_close();
//set_time_limit(0);
readfile($file);
}
exit(0);
我没有流媒体内容方面的经验,但使用JS来做这件事可能更好。这是因为JS和AJAX可以动态地改变事情,这是PHP无法做到的。@MonkeyZeus是的,现在我学到了这一点,谢谢。我没有流媒体内容方面的经验,但使用JS来实现这一点可能更好。这是因为JS和AJAX可以动态地改变事情,这是PHP无法做到的。你正在经历所谓的会话锁定。@MonkeyZeus是的,现在我学到了这一点,谢谢请注意,这只适用于PHP>=7.x,因为OP可能有一个未升级的托管提供程序。请注意,这只适用于PHP>=7.x,因为OP可能有一个未升级的托管提供程序。谢谢,这正是我需要的。学到了一些新东西,干杯:谢谢你,这正是我需要的。学到了新东西,干杯: