gpt4 book ai didi

laravel - 如何处理 laravel 5 中的私有(private)图像?

转载 作者:行者123 更新时间:2023-12-04 01:20:58 25 4
gpt4 key购买 nike

我是 Laravel 的新手,并试图存储私有(private)图像,以便只有经过身份验证的用户才能访问它们。首先,我将图像存储在 Public/UserImages 文件夹中。但是在这里,未经身份验证的用户也可以访问所有图片,方法是转到 Inspect Element of chrome,然后更改用户 ID。请帮帮我...

最佳答案

在这里如何在 Laravel 5.7 中做到这一点

要拥有私有(private)文件(图像),您需要通过 提供文件。路线 => Controller 流动。您的身份验证中间件将处理身份验证和权限。如果需要进一步授权,您可以在 Controller 中处理它。

所以首先我们设置一条路线:

在这里我们可以有处理所有 的一条路线我们的文件[我个人不喜欢那样]。
我们可以使用这样的路由来做到这一点(它就像一个通配符)。

Route::get('/storage/{filePath}', 'FileController@fileStorageServe')
->where(['filePath' => '.*'])

你也可以这样命名:
Route::get('/storage/{fileName}', 'FileController@fileStorageServe')
->where(['fileName' => '.*'])->name('storage.gallery.file');

否则 我们创建 每种文件类型/类别的路径 : ( 优势 :您将能够更好地控制可访问性。(每种路线和资源类型及其规则。如果您想使用通配符路线(让我这么称呼它)实现这一目标)需要有条件 block (if else,处理所有不同的情况。这是不必要的操作[直接进入正确的 block ,当路由分开时更好,加上它可以让您更好地组织权限处理])。
Route::get('/storage/gallery/{file}', 'System\FileController@getGalleryImage')
->name('storage.gallery.image');

我们现在设置了路由 Controller / Controller

外卡一号
  <?php
public function fileStorageServe($file) {
// know you can have a mapping so you dont keep the sme names as in local (you can not precsise the same structor as the storage, you can do anything)

// any permission handling or anything else

// we check for the existing of the file
if (!Storage::disk('local')->exists($filePath)){ // note that disk()->exists() expect a relative path, from your disk root path. so in our example we pass directly the path (/.../laravelProject/storage/app) is the default one (referenced with the helper storage_path('app')
abort('404'); // we redirect to 404 page if it doesn't exist
}
//file exist let serve it

// if there is parameters [you can change the files, depending on them. ex serving different content to different regions, or to mobile and desktop ...etc] // repetitive things can be handled through helpers [make helpers]

return response()->file(storage_path('app'.DIRECTORY_SEPARATOR.($filePath))); // the response()->file() will add the necessary headers in our place (no headers are needed to be provided for images (it's done automatically) expected hearder is of form => ['Content-Type' => 'image/png'];

// big note here don't use Storage::url() // it's not working correctly.
}

每条路线一条

(差别很大,是参数,现在只引用文件名,而不是存储磁盘根目录的相对路径)
<?php
public function getCompaniesLogo($file) {
// know you can have a mapping so you dont keep the sme names as in local (you can not precsise the same structor as the storage, you can do anything)

// any permission handling or anything else

$filePath = config('fs.gallery').DIRECTORY_SEPARATOR.$file; // here in place of just using 'gallery', i'm setting it in a config file

// here i'm getting only the path from the root (this way we can change the root later) / also we can change the structor on the store itself, change in one place config.fs.

// $filePath = Storage::url($file); <== this doesn't work don't use

// check for existance
if (!Storage::disk('local')->exists($file)){ // as mentionned precise relatively to storage disk root (this one work well not like Storage:url()
abort('404');
}

// if there is parameters [you can change the files, depending on them. ex serving different content to different regions, or to mobile and desktop ...etc] // repetitive things can be handled through helpers [make helpers]

return response()->file(storage_path('app'.DIRECTORY_SEPARATOR.($file))); // the response()->file() will add the necessary headers in our place
}

现在您可以通过形成正确的 url 进行检查(转到文件名之后的存储副本,并形成您的路线。它应该向您显示图像)

剩下最后一件事:

如何在 View 中显示

外卡一号
<img src="{{route('routeName', ['fileParam' => $storageRelativePath])}}" />

请注意 routeName在上面的示例中,这里将是 storage.file , 和 fileParam将是 filePath . $storageRelativePath 例如,您从数据库中获取(通常就是这样)。

每条路线
<img src="{{route('routeName', ['fileName' => basename($storageRelativePath)])}}" />

相同,但我们只提供文件名。

备注:
发送此类响应的最佳方式是使用 响应()->文件(); .
这就是您将在 5.7 文档中找到的内容。
的表现明智图片::make($storagePath)->response(); .除非您需要即时修改它。

您可以查看我的文章中号: https://medium.com/@allalmohamedlamine/how-to-serve-images-and-files-privatly-in-laravel-5-7-a4b469f0f706

关于laravel - 如何处理 laravel 5 中的私有(private)图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28562908/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com