Laravel 5:无法显示上传的图像并且文件权限 [英] Laravel 5: Uploaded image can't be displayed and file permission

查看:202
本文介绍了Laravel 5:无法显示上传的图像并且文件权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 5项目中,我将图像上传到公用文件夹的子文件夹中.例如:public/uploads/logos/imagename.jpg.图片已成功上传,但是我遇到两个问题:

In my Laravel 5 project, I uploaded image to my sub folders of the public folder. Example: public/uploads/logos/imagename.jpg. The image is uploaded successfully but I have two problems:

  1. 我无法使用<img src="uploads/logos/imagename.jpg">在视图中显示图像.该错误表明找不到该文件.

  1. I can't display the image in the view using <img src="uploads/logos/imagename.jpg">. The error says that file is not found.

使用下面的代码删除图像时,我遇到了另一个文件权限问题:

When using below code to delete the image, I got another problem of file permission:

$ path ="uploads/logos/imagename.jpg"; if(file_exists($ path)){取消链接($ path); }

$path ="uploads/logos/imagename.jpg"; if(file_exists($path)) { unlink($path); }

请告知如何正确上传,删除上传的文件并显示上传的图像.

Please kindly advise how to upload properly, delete uploaded file, and display the uploaded image.

谢谢,娜莲

推荐答案

有一些laravel和第三方功能可以为您解决此问题,为了更好地解决问题,让我们解决一个常见问题,上传用户头像:

There are several laravel and third party features that can solve this issue for you, for a better exponation, let's solve a common problem, uploading an user avatar:

您具有以下形式:

<form method="POST" action="/avatar" accept-charset="UTF-8" enctype="multipart/form-data">
{{ csrf_field() }}
        <div>
           <input name="avatar" type="file">
        </div>
<div class="margin-top-10">
    <input type="submit" class="btn green" value="Submit" /> 
</div>

是上传文件的形式,请注意,您需要放置accept-charsetenctype属性,以便文件字段与后端一起使用.之后,您必须让您的控制器上载化身,假设您拥有ProfileController类,就像下面的操作一样:

that is the form to upload a file, note that you need to put accept-charset and enctype attributes in order to the file field works with your backend. After this, you have to make your controller to upload the avatar, imagine you have the ProfileController class, simply as this action:

public function set_profile_picture(Request $request) {
        $user = $request->user();
        if (!$request->hasFile('avatar')) {
            Flash::error('Avatar not found');
            return redirect('/profile');
        }
        $path = $request->file('avatar')->store('avatars');
        $user->avatar = $path;
    $user->save();
    return redirect('/profile');
}

检查是否可以使用$request->file(avatar)从前端表单获取文件,完成后,您需要定义路由:Route::post('/avatar', 'Auth\\ProfileController@set_profile_picture');

Check that you can get the file from the frontend form by using $request->file(avatar), with that done, you need to define the route: Route::post('/avatar', 'Auth\\ProfileController@set_profile_picture');

最后,您需要做另一件事来显示上传的图片,您必须选择,将文件存储在服务器中是由Laravel中的Storage Facade进行的,因此,您会发现您无法提供公开访问这些文件,因为它们位于名为storage的文件夹中,因此您始终可以在包含化身的文件夹中建立符号链接,以便在具有前置控制器和所有这些东西的public文件夹中对其进行访问,但是如果您使用的是git等VCS,则此符号链接将来可能会给您带来问题,最安全的方法是在控制器中定义另一个操作以访问存储中的私有文件,以下是更好的方法方法:

And finally, you need to make another thing to show the picture uploaded, you have to choices, storing the file in the server is made by the Storage Facade in Laravel, so, you will find out that you cannot give public access to these files because they are in a folder named storage, you can always make a symbolic link to the folder containing the avatars to have access to them in the public folder where you have the front controller and all those stuffs, but if you are using some VCS like git, this symlink could bring you problems in the future, the most secure way to do this is to define another action in your controllers to access a private file in your storage, the following is the better way to do this:

 Route::get('avatars/{filename}', function ($filename) {

   $path = storage_path() . '/app/avatars/' . $filename;
   if(!File::exists($path)) abort(404);

   $file = File::get($path);
   $type = File::mimeType($path);

   $response = Response::make($file, 200);
   $response->header("Content-Type", $type);

   return $response;
});

您可以在laravel官方文档中找到有关Storage Facade的更多信息,取消链接的操作很简单\Storage::Delete('/path/to/file.jpg');

You can find more information about the Storage Facade in the laravel official documentation, to unlink a file is simple \Storage::Delete('/path/to/file.jpg');

希望它对您有帮助,最好! ;)

Hope it helps you this little exponation, bests! ;)

这篇关于Laravel 5:无法显示上传的图像并且文件权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆