如何在 Laravel 中存储图像而不是 base64

再会。拜托,我需要你的帮助。建立一个 laravel 网站,其中 tinymce 在一些文本区域中实现。挑战在于,如果在编辑器中上传图像,它们将存储为 base64 编码。这会减慢服务器的速度。我不得不在我的数据库中将我的数据类型更改为 longtext。如何存储图像而不是 base64?以及如何读取存储的图像。


我的代码显示在我的控制器下方


public function create(Request $request){



        $categories = BlogCategory::all();

        $tags = Tag::all();


        if($request->isMethod('post')){


            //dd($request);

            $data = $request->except('name');

            $post = new Post;

            //Title

            $post->title = $request->title;

            //Slug

            $post->publish_date = new Carbon;

            $slug = $this->createSlug($request->title);

            $post->slug = $slug;


            //Category

            if($request->category_id == "Choose Category")

            {

                Session::flash('failure','Please Select A Category To Proceed!');

                return redirect()->back();

            }else{

                $post->category_id = $request->category_id;

            }



            //Body

            $post->body = $request->body;


            //Author

            if(isset($request->author)){

                $post->author = $request->author;

                $post->author_slug = Str::slug($post->author,'-');

            }else{

                $post->author = "";

                $post->author_slug = "";

            }


            //User ID

            $post->user_id = Auth::user()->id;


            //Keywords

            if(isset($request->keywords)){

                $post->keywords = $request->keywords;

            }else{

                $post->keywords = "";

            }


            //Description

            if(isset($request->description)){

                $post->description = $request->description;

            }else{

                $post->description = "";

            }


拉莫斯之舞
浏览 132回答 1
1回答

慕莱坞森

您的标题/描述说明了一些内容,但您的代码说明了其他内容。要将文件存储在数据库中,列类型必须是 BINARY/BLOB。要将文件名存储在数据库中并将文件存储在磁盘上,列类型应该是相对于最大文件名长度的 VARCHAR。除非文件很小,否则不要将文件转换为 base64,因为它们的大小会增加大约 x3 倍。要将文件存储在数据库中,您可以使用此代码。在您的控制器内部:if ($request->hasFile('file')){    // If you want to resize the image, use the following method to get temporary file's path.    $request->file('file')->getPathName();     // `get()` retrieves file's content in binary mode    $post->image = request->file('file')->get(); }
打开App,查看更多内容
随时随地看视频慕课网APP