简体   繁体   中英

unable to upload file greater than 2MB in laravel

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');

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

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'); 
echo phpinfo();

在此输入图像描述

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

; Maximum allowed size for uploaded files
upload_max_filesize = 4M

In my case I modified the php.ini file under CLI path, as well my php.ini file under my apache2.

You can find the php.ini file used with the following command:

# php --ini

Configuration File (php.ini) Path: /etc/php/8.0/cli
Loaded Configuration File:         /etc/php/8.0/cli/php.ini

you can set uploaded image size "max = 10000"

$request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:10000',
        ]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM