DEV Community

Robert Look
Robert Look

Posted on

How to upload image using ckeditor in laravel 8

we need to store long text, article content, product summary, and different tag content, etc, with the description in our database, we use the CKEditor editors. this editor's use of text stylish is easy to use any content.

I write a very simple example of image uploading with laravel step by step so you can easily use it in your laravel 8. Ckeditor is the most powerful tool for the content editor. so if you have an image upload option also available then it awesome.

How to upload image using ckeditor in laravel 8

<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
use Response;
class ArticleController extends Controller
{
    public function index()
    {
        return view('article-form');
    }
    public function uploadImage(Request $request) {     
    if($request->hasFile('upload')) {
            $originName = $request->file('upload')->getClientOriginalName();
            $fileName = pathinfo($originName, PATHINFO_FILENAME);
            $extension = $request->file('upload')->getClientOriginalExtension();
            $fileName = $fileName.'_'.time().'.'.$extension;

            $request->file('upload')->move(public_path('images'), $fileName);

            $CKEditorFuncNum = $request->input('CKEditorFuncNum');
            $url = asset('images/'.$fileName); 
            $msg = 'Image uploaded successfully'; 
            $response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";

            @header('Content-type: text/html; charset=utf-8'); 
            echo $response;
        }
    }   
}
?>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)