使用 php 在 webroot 外部读取视频文件

Read a video file outside webroot using php

我正在使用 moodle 3.1+。我正在尝试使用 readfile 函数读取 webroot 之外的文件。该文件是一个视频文件,但保存时没有任何扩展名。现在,当我执行脚本时出现错误 "video can't be played because file is corrupt"。

header('Content-Type: video/mp4');
readfile('../../../../moodledata/filedir/17/73/17735b4721faa7d59e1fd8a2300135a3f04c9ea6');

请帮忙

不确定您的目标,但我认为以这种方式访问​​ moodledata 不是一个好主意。你如何处理权限?你将如何处理文件被删除的情况?

为什么不通过 moodle 本身提供服务呢?我假设您想提供文件下载? /ressource/view.php 将是一个很好的切入点。您可以根据使用 view.php 重定向到特定存储库的重定向构建自定义脚本。

$path = '/'.$context->id.'/mod_resource/content/'.$resource->revision.$file->get_filepath().$file->get_filename();
$fullurl = moodle_url::make_file_url('/pluginfile.php', $path, $displaytype == RESOURCELIB_DISPLAY_DOWNLOAD);
redirect($fullurl);

我能够使用以下代码显示该文件。

$fs = get_file_storage();
$file = $fs->get_file_by_hash(sha1($filepath));
send_stored_file($file, 0, 0); 

其中 $filepath 是动态生成的,如下所示

"/$context->id/mod_lesson/mediafile/0".$lesson->mediafile