在 php 中显示文件而不阻止应用程序

问题描述 投票:0回答:1

我正在 Apache2 上提供一个 php 应用程序。在一个函数中,我只想使用 php 脚本显示媒体,而不是将媒体托管在服务器上的公共文件夹中。

这是因为我希望能够添加功能来控制登录用户的权限,并首先检查文件扩展名等。

我尝试过fpassthrureadfile,这两个函数都完全阻止了应用程序。尤其是当视频 > 10mb 时。

这是代码的一个版本:

display_file.php?文件名=foo.jpg

if (empty($_GET['filename']))
{
    die(http_response_code(500)):
}

$filename = $_GET['filename'];
$extension = file_extension($filename); // .jpg|.mp4 etc
$mime = mime($ext); // get mine_type
$uri = '/foo/bar/'.$filename;


// Only allow sertain extensions
if (!in_array($ext, ALLOWED_FILE_EXTENSIONS)) // ['jpg','png','mp4'...]
{
    die(http_response_code(403));
}

// File doesn't exist
if (!file_exists($uri))
{
    die(http_response_code(404));
}

$size = filesize($uri);
$start = 0;
$end = $size - 1;
$time = filemtime($uri);
$expires = gmdate('D, d M Y H:i:s \G\M\T', $time + (60 * 60));
$modified = gmdate('D, d M Y H:i:s \G\M\T', $time);

//$fp = fopen($uri, 'rb');

header('Pragma: cache');
header('Cache-Control: max-age=31536000');
header('Expires: '.$expires);
header('Content-Type: '.$mime);
header('Content-Length: '.$size);
header('Accept-Ranges: bytes');
header("Content-Range: bytes $start-$end/$size");
header('Last-Modified: '.$modified);

//fpassthru($fp);
readfile($uri);

如果我使用 Apache2 直接通过公共文件夹显示文件,则应用程序不会被阻止。

有没有其他方法可以在不阻塞整个应用程序的情况下通过 php 用户检查、扩展等服务/显示文件?

php apache2 readfile
1个回答
0
投票

您的分析/描述不清楚。 “阻止该应用程序”是什么意思?你是如何测量的?

我怀疑这里发生的情况是会话被阻止,而不是应用程序被阻止,尽管内容文件被锁定可能存在问题。

您向我们展示的代码不会阻止会话 - 没有会话。但代码显然不完整。在

session_write_close()
之前添加
readfile()
可以解决(或使用非阻塞会话处理程序)。

内容文件锁定的可能性较小 - 但您没有告诉我们该文件在什么操作系统上运行,因此我们无法告诉您如何进一步调查。

© www.soinside.com 2019 - 2024. All rights reserved.