欢迎您光临自学哈网,只为分享网络知识教程,供大家学习参考!

「自学哈网」在Laravel中如何获取到文件夹的URL?

作者 : 自学哈 本文共615个字,预计阅读时间需要2分钟 2023-05-27 共70人阅读
也想出现在这里? 联系我们

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

诀窍是使用 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>

 

本站声明:
本站所有资源来源于网络,分享目的仅供大家学习和交流!如若本站内容侵犯了原著者的合法权益,可联系邮箱976157886@qq.com进行删除。
自学哈专注于免费提供最新的分享知识、网络教程、网络技术的资源分享平台,好资源不私藏,大家一起分享!

自学哈网 » 「自学哈网」在Laravel中如何获取到文件夹的URL?
也想出现在这里? 联系我们
© 2022 Theme by - 自学哈网 & WordPress Theme. All rights reserved 浙ICP备2022016594号