laravel视频不在公用文件夹中
我有一份laravel申请(5.4) 每个用户在/public下都有一个文件夹,管理员可以通过FTP上传一些视频供用户观看。每个用户都有不同的视频laravel视频不在公用文件夹中,laravel,storage,public,protected,Laravel,Storage,Public,Protected,我有一份laravel申请(5.4) 每个用户在/public下都有一个文件夹,管理员可以通过FTP上传一些视频供用户观看。每个用户都有不同的视频 我现在的问题是,公用文件夹中的所有内容都可以访问。如果用户未登录,也可以。如何避免这种情况?让我们假设有两个用户使用以下用户名: 约翰 莎拉 然后您将有2个文件夹: storage/app/john storage/app/sarah 现在要访问john的文件,您的routes/web.php中应该包含以下内容: Route::get('/f
我现在的问题是,公用文件夹中的所有内容都可以访问。如果用户未登录,也可以。如何避免这种情况?让我们假设有两个用户使用以下用户名:
- 约翰
- 莎拉
storage/app/john
storage/app/sarah
Route::get('/file/{username}/{name}','FilesController@getFile');代码>
然后在filecontroller
中添加此函数:
public function getFile($username,$name){
if(!Auth::user()){
//user is not logged in
return response(null,403)
}
$auth_user = Auth::user()->username;
if($username === $auth_user){
//continue with the request
$exists = Storage::disk('local')->exists($username .'/'. $name);
if(!$exists){
// file not found
return response(null,404);
}
$file = Storage::get($username .'/'. $name);
return response($file);
}else{
// the user doesn't have access
}
}
我没有测试这个,所以如果你有问题,请告诉我
您需要将它们存储为私有文件,然后当用户点击某个链接时,您确保该链接已被记录,并且可以访问解决方案的linkThx。每个登录用户都可以访问自己的视频,但不能访问其他用户的视频。问题是,当我将url更改为mp4文件时,如果没有人登录,我也可以看到它。将其移动到存储文件夹,我在登录时也无法访问它。您只能通过此功能访问文件,直接链接到该文件将不起作用,因为它不在storage/app/public
folder中。那么如何才能通过此return response()->文件将文件传递到html格式的视频或img标记(存储路径('app',$username./'.$name));