【问题标题】:How to catch any link that came from upload/ in laravel 5?如何在 laravel 5 中捕获来自上传/的任何链接?
【发布时间】:2016-08-13 20:21:06
【问题描述】:

我是 laravel 5.2 的新手,我只是想问一下你如何捕获来自 uploads 的链接,例如:http://sitename.com/uploads/59128.txt?如果他们试图访问来自uploads/{any filename} 的任何路由或链接,我想将他们重定向到登录页面。

【问题讨论】:

  • 澄清问题!您的意思是要拦截指向特定文件夹中文件的链接吗?该文件是否存在于您的公用文件夹中?你的文件会匹配/uploads/*.txt的模式吗?

标签: laravel authentication routes laravel-5.2


【解决方案1】:

@xerwudjohn 很简单,你不能。 当这个文件在公用文件夹中时,任何人都可以在没有登录的情况下访问它。 我尝试了几分钟的一种方法,创建一条新路线:

Route::group(['middleware' => ['web', 'auth']], function () {
   Route::get('/download/{id}', 'DownloadController@showFile');
});

在DonwloadController中创建函数showFile

public function showFile($id)
{
    return redirect('/image/'.$id.'.txt');
}

或使用模型从任何表中读取 uniqueIds 并获取真实文件名。 干杯

【讨论】:

    【解决方案2】:

    是的,您可以通过使用身份验证中间件保护您的路由来实现, 做个小FileController

    class FileController extends Controller {
        public function __construct()
        {
            $this->middleware('auth');
        }
    
        public function getFile($filename)
        {
            return response()->download(storage_path($filename), null, [], null);
        }
    }
    

    然后在routes.php

    Route::get('file/{filename}', 'FileController@getFile')->where('filename', '^[^/]+$');

    就是这样。现在,经过身份验证的用户可以通过调用http://yoursite.com/file/secret.jpg 从存储文件夹(但不能从其子文件夹)下载文件。添加您可以在图像标签的 src 属性中使用此 URL。

    答案的原始source

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-01-01
      • 2017-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多