亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Laravel 中存儲圖像而不是 base64

如何在 Laravel 中存儲圖像而不是 base64

PHP
拉莫斯之舞 2022-10-14 10:38:41
再會。拜托,我需要你的幫助。建立一個 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 = "";            }
查看完整描述

1 回答

?
慕萊塢森

TA貢獻1810條經驗 獲得超4個贊

您的標題/描述說明了一些內容,但您的代碼說明了其他內容。

  • 要將文件存儲在數據庫中,列類型必須是 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(); 

}


查看完整回答
反對 回復 2022-10-14
  • 1 回答
  • 0 關注
  • 104 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號