在Laravel中如何获取到文件夹的URL?

(浏览器不能直接访问文件)——您仍然可以通过函数提供对这些文件的链接访问。

诀窍是使用 php 函数readfile()

这允许您随后保护文件,并对它们应用权限 – 即只有特定用户可以访问这些文件,用户只能访问他们自己的文件等。

这是允许登录用户访问文件的一种方法的示例

像这样的路线:

Route::get('/view/{$file}', ['as' => 'viewfile', function($file) {
     // Ensure no funny business names to prevent directory transversal etc.
     $file = str_replace ('..', '', $file);
     $file = str_replace ('/', '', $file);
     // now do the logic to check user is logged in
     if (Auth::check())
     {
            // Serve file via readfile() - we hard code the user_ID - so they
            // can only get to their own files
           readfile('../uploads/'.Auth::user()->id.'/'.$file);
     }
}]);

然后在某处的视图文件中,您可以像这样链接到该文件:

<p>Here is a link to your file: {{ URL::route('viewfile', ['your_file_name.jpg']) }}</p>

 

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容