Laravel访问公用文件夹外部的图像

编程入门 行业动态 更新时间:2024-10-28 20:24:11
本文介绍了Laravel访问公用文件夹外部的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要将图像存储在后端以供已登录的用户使用.存储的图像需要受到保护,并且从外部(公共)看不到.我为此选择了一个存储"文件夹.

I need to store images in a backend for logged in users. The stored images need to be protected and not visible from the outside (public). I choosed a "storage" folder for this.

我在控制器中想到了这个:

I came up with this in my Controller:

public function update(Request $request, $id) { //Show the image echo '<img src="'.$_POST['img_val'].'" />'; //Get the base-64 string from data $filteredData=substr($_POST['img_val'], strpos($_POST['img_val'], ",")+1); //Decode the string $unencodedData=base64_decode($filteredData); //Save the image $storagepath = storage_path('app/images/users/' . Auth::user()->id); $imgoutput = file_put_contents($storagepath.'/flyer.png', $unencodedData); return view('backend.flyers.index')->withImgoutput($imgoutput) //->withStoragepath($storagepath); }

在点击保存按钮后触发了update(),我可以在视图中看到该图像,并且该图像也存储在我的文件夹中(当前用户= 10)"storage/app/images/users/10 /flyer.png"

after hitting the save button, which triggers the update() I am able to see the image in my view, and it is also stored in my folder (current users=10) "storage/app/images/users/10/flyer.png"

我的问题是如何访问图像路径? 我想用img src =">显示存储的图像.我不知道在"src = ..."里面放什么

my question is how can I access the image path? I want to show the stored image with img src="">. I have no idea what to put inside "src= ..."

推荐答案

在处理Web应用程序中的用户文件上传时,主要方面是关于用户内容的安全性. 人们应该使用安全的方式在Web应用程序中上载用户的私人文件.

While dealing with user file uploads in web applications, the major aspect is about user's content's security. One should use secure way to upload private files of a user in web applications.

与您的情况一样,您要访问公用文件夹之外的用户图像. 可以通过以下最安全的方式完成此操作.

As in your case, you want to access user's image outside public folder. This can be done in a most secure way as given below.

首先在Laravel的根目录(公用文件夹所在的目录)中创建一个目录,让目录的名称为uploads.使用此目录上载私人用户文件.

First of all create a directory right in the root directory of Laravel (where the public folder is located), let the directory's name be uploads. Use this directory to upload private user files.

对于图像,在上载目录中创建另一个目录,在上载目录中创建为uploads/images/,这样您就可以为不同类型的文件使用不同的存储位置.

In the case of images create an another directory inside uploads as uploads/images/ inside uploads directory so that you can have a different storage locations for different type of files.

请记住以不同的名称(没有扩展名)将图像上传到ima​​ges目录中,这样看起来就好像没有扩展名的文件一样. 将文件名及其扩展名保留在数据库中,以后可用于保留图像的位置.

Remember to upload the image in images directory with a different name and without their extensions so that it looks like a extension-less file. Keep the file name and its extension in the database which can be used later to retain image's location.

现在,您需要创建一条单独的路线来显示用户的图像.

Now you need to create a separate route to show user's image.

Route::get('users/{id}/profile_photo', 'PhotosController@showProfilePhoto')->name('users.showProfilePhoto');

PhotosController.php

PhotosController.php

class PhotosController extends Controller { private $image_cache_expires = "Sat, 01 Jan 2050 00:00:00 GMT"; public function showProfilePhoto($id) { $user = User::find($id); $path = base_path() . '/uploads/images/'; if($user && $user->photo) // Column where user's photo name is stored in DB { $photo_path = $path . $user->photo; // eg: "file_name" $photo_mime_type = $user->photo_mime_type; // eg: "image/jpeg" $response = response()->make(File::get($photo_path)); $response->header("Content-Type", $photo_mime_type); $response->header("Expires", $this->image_cache_expires); return $response; } abort("404"); } }

访问名为-users.showProfilePhoto的路由后,PhotosController-showProfilePhoto($user_id)内部的上述方法将立即运行.

The method above inside PhotosController - showProfilePhoto($user_id) will run as soon as you access the route named - users.showProfilePhoto.

您的HTML代码将如下所示.

Your HTML code will look like this.

<img src="<?php echo route('users.showProfilePhoto', array('id' => $user->id)); ?>" alt="Alter Text Here">

以上代码将像超级按钮一样工作,并且无需向公众声明/发布正确的图像路径即可将图像显示给用户. 据我说,这是处理Web应用程序中文件上传的安全方法.

The above code will work like a charm and the image will be shown to the user without declaring/publishing the proper image path to public. According to me this is the secure way to deal with file uploads in web applications.

更多推荐

Laravel访问公用文件夹外部的图像

本文发布于:2023-07-12 07:22:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1096840.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   图像   Laravel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!