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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器