duan0821 2016-04-14 10:37
浏览 640
已采纳

使用Laravel将图像路径保存到数据库

I have a function that takes submitted form data which is an image, then validates it and it deletes an old image if one already exists. It currently does that and stores the image on in the storage folder.

I am now trying to save the URL of the image to the database. I have seen examples where they do not save the path to the database but would it be best to do so?

Here is my function

public function postAvatarUpload(Request $request)
    {
         $this->validate($request, [
            'image' => 'required|image|max:3000|mimes:jpeg,jpg,bmp,png',
        ]);
        $user = Auth::user();
        $usersname = $user->username;
        $file = $request->file('image');
        $filename = $usersname . '.jpg';
        if (Storage::disk('local')->has($filename)) {
            Storage::delete($filename);
        }
           Storage::disk('local')->put($filename, File::get($file));
           $avatarPath = Storage::url($filename);
            Auth::user()->update([
                'image' => $avatarPath,
            ]);

        return redirect()->route('profile.index', 
                ['username' => Auth::user()->username]);
    }
  • 写回答

2条回答 默认 最新

  • dongzangchui2072 2016-04-14 12:09
    关注

    I mostly store images people upload to a subfolder in the public folder from Laravel. The image can then be requested with only your webserver doing the work and not having to serve it via PHP.

    For images like an avatar I create a folder structure based on the model name and id of the model. Like: laravel_folder/public/images/user/13/avatar.jpg.

    Because the path and url are based on the modelname and id, you can easily generate a direct url and path to it. Downside is that the url and path to avatars of other users is predictable, so don't use it for sensitive images.

    I see you accept an image in multiple formats (jpg,png,bmp). When you save the image, you save it as an jpg image. You should use an image manipulation library like Intervention, or get the extension of the file with pathinfo($file, PATHINFO_EXTENSION), otherwise the file won't be readable because the image extension and the image contents don't match.

    Also think about the image size. Users can upload an image in poster size, while you only need a small image. Use an image manipulation library to create a smaller version image of the original image.

    If you save the original image in the format you get from the user, then just check all extensions if they exist. One way to do it would be:

    $extensions = [ 'jpg', 'jpeg', 'png', 'bmp' ];
    
    $user = Auth::user();
    
    $imagePath = "{public_path()}/images/{class_basename($user)}/{$user->id}/";
    $imageName = "avatar";
    
    // go through all extensions and remove image if it exists
    foreach($extensions as $ext)
    {
        $imageFullPath = "{$imagePath}/{$imageName}.{$ext}";
    
        if( file_exists($imageFullPath) )
        {
            @unlink($imageFullPath);
            break;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色