3

Currently, I am doing a CMS in laravel and I am trying to upload a file in laravel more than 2MB and It displays that image failed to upload.

my controller code.

    $this->validate($request ,[
        'title' => 'required',
        'featured' => 'required|image',
        'content' => 'required',
        'category_id' => 'required',
        'tags' => 'required'
    ]);

    $featured = $request->featured;

    $featured_new_name = $featured->getClientOriginalName();

    $featured->move('uploads/posts', $featured_new_name);

    $post = Post::create([
        'title' => $request->title,
        'slug' => str_replace(' ', '-', $request->title),
        'featured' => 'uploads/posts/' . $featured_new_name,
        'content' => $request->content,
        'category_id' => $request->category_id,
        'user_id' => Auth::id()

    ]);   

     $post->tags()->attach($request->tags);

    Session::flash('success', 'Post Created Sucessfully.');

    return redirect()->route('posts');
Sa Roz
  • 63
  • 1
  • 6
  • 6
    Possible duplicate of [Change the maximum upload file size](https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size) – Mike Aug 14 '18 at 04:45

3 Answers3

4

Try increasing the following values in php.ini, for example:

memory_limit = 32M
upload_max_filesize = 24M
post_max_size = 32M

or check this link

Dhruv Raval
  • 1,447
  • 1
  • 6
  • 15
3
echo phpinfo();

enter image description here

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Maximum allowed size for uploaded files
upload_max_filesize = 4M
Ragupathi
  • 469
  • 4
  • 19
  • If you have made changes "4M" in your php.ini file then just restart your apache service on server then check upload_max_filesize by using phpinfo() function in the browser, you will definetely see updated value "4M". Thanks. – Kamlesh May 04 '21 at 09:01
3

Increasing the memory limit in php.ini is the best idea. if you just want to increase memory limit in this function, then use this code on your function.

ini_set('memory_limit', '4096M'); 
Tpojka
  • 6,832
  • 2
  • 29
  • 38
Hasan Hafiz Pasha
  • 1,254
  • 2
  • 15
  • 21