【问题标题】:Can I use readfile() with videos in php?我可以在 php 中对视频使用 readfile() 吗?
【发布时间】:2017-08-21 12:47:19
【问题描述】:

我正在寻找一种方法来保护我的视频而不是 DRM,所以我尝试使用会话而不是视频的直接链接,然后我检查它,如果会话是真的,我使用 fopen/ 运行视频读取文件()。

但是视频不能是种子,它只是像这个例子一样在生命周期中工作:

Here is a link from the site I work on.

我可以使用任何东西使其可搜索吗?

    $file = base_path(). '/uploads/test.mp4';

    $size = File::size($file);
    $myfile = fopen($file, 'rb');

    echo fread($myfile, $size);

【问题讨论】:

  • 更好的方法是使用路由来流式传输视频并使用它
  • 那是怎么回事?
  • 只需创建一个路由并将您的视频获取代码包装到其中,然后将该路由调用到您的<source src="">。如果您需要完整的代码,请告诉我
  • 这就是我真正所做的! <source src="{{route('get-video')}}"> 而这条路由中的代码就是帖子中的代码

标签: php html laravel video-streaming html5-video


【解决方案1】:

你的路线喜欢:

Route::get('get-video/{videoName}', 'YourController@getVideo')->name('getVideo');

您的控制器代码如:

public function getVideo($videoName)
{

    $fileContents = Storage::disk('local')->get("uploads/".$videoName);
    $response = Response::make($fileContents, 200);
    $response->header('Content-Type', "video/mp4");
    return $response;
}

您的看法如下:

<video width="320" height="240" controls>
        <source src="{{ route('getVideo', 'test.mp4')  }}" type="video/mp4">
        Your browser does not support the video tag.
</video>

您还需要在您的根.htaccess 文件中添加以下行:

AddType video/webm .webm

注意:在此更改之后,您仍然面临音频问题,然后也需要将以下行放入.htaccess:

Add type video/ogg .ogv 
Add type audio/ogg .oga

【讨论】:

  • @ahmedyasser:检查这个答案并告诉我
  • 我刚刚在我的目录文件和Here is the result 中编写了您的代码。同样的问题出现了
  • 好的,500 Internal Server Error 表示代码有问题。你能调试一下吗
  • 我还发现 No video with supported format and MIME type found 意味着您需要在根目录中使用 .htaccess 来允许此类视频:AddType video/mp4 .mp4
  • @ahmedyasser:有结果吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
相关资源
最近更新 更多