unable to upload file greater than 2MB in laravel

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Mastering File Uploads in Laravel: Troubleshooting Files Larger Than 2MB

As a senior developer working with large-scale Content Management Systems (CMS) in Laravel, file uploads are a frequent source of frustration. When users attempt to upload files larger than the standard limits—such as an image exceeding 2MB—and receive failure messages, it often points not at an error in your Eloquent model or controller logic, but rather at limitations imposed by the underlying server configuration.

This post will walk you through the common causes of this issue and provide practical, robust solutions to successfully handle large file uploads within your Laravel application.

The Root Cause: Server Limits vs. Laravel Logic

When a file upload fails silently or with a generic error message in Laravel, the problem is usually occurring before the request even reaches your controller method. Laravel itself provides excellent tools for validation and storage management, but it relies on the server environment (PHP) to actually process the raw incoming data stream.

The most common culprits for file upload failures are PHP's configuration directives: upload_max_filesize and post_max_size. If these limits are set too low (e.g., default 2MB), any file larger than that will be rejected by the web server before your Laravel code ever sees it.

Solution 1: Adjusting PHP Configuration (The Foundation)

Before diving into complex code changes, you must ensure your server environment can handle the required file sizes. This is the most critical step. You typically need to edit your php.ini file or use a .htaccess file if you have access to the hosting environment.

Ensure these directives are set appropriately for your needs:

upload_max_filesize = 16M  ; Increase this value to accommodate larger files (e.g., 16 Megabytes)
post_max_size = 32M       ; This must be equal to or larger than upload_max_filesize
memory_limit = 256M      ; Ensure memory limits are also sufficient for processing large uploads

After making these changes, you must restart your web server (like Apache or Nginx) for the changes to take effect. Properly configuring this layer ensures that Laravel can receive the file stream intact.

Solution 2: Robust File Handling in Your Controller

While fixing the server limits solves the upload failure, we need to review how you handle the file within your controller to ensure data integrity and proper storage. In your provided example, the logic for moving the file looks generally correct, but we can make it more resilient and align it with Laravel's best practices regarding file storage.

When dealing with file attachments, especially in a CMS context, storing files directly in the public directory is generally discouraged for security and scalability reasons. It is much better practice to use the Laravel Storage facade, which abstracts file operations away from the filesystem. As detailed in resources provided by the official platform, leveraging dedicated storage solutions enhances your application's architecture significantly.

Here is a refined approach focusing on using the store method:

use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;

class PostController extends Controller
{
    public function store(Request $request)
    {
        // 1. Validation ensures the file type and existence are checked first
        $validated = $request->validate([
            'title' => 'required',
            'featured' => 'required|image|max:16384', // Added max size validation here for extra layer of defense (if server limits fail)
            'content' => 'required',
            'category_id' => 'required',
            'tags' => 'required,array', // Ensure tags are handled as an array
        ]);

        // 2. Handle the file upload using Laravel Storage facade
        $file = $request->file('featured');

        if ($file) {
            // Store the file in the configured disk (e.g., 'public' disk)
            // The path will be stored relative to your configured disk root.
            $path = $file->store('posts', 'public'); 
        } else {
            return redirect()->back()->with('error', 'No file was uploaded.');
        }

        // 3. Create the Post record
        $post = Post::create([
            'title' => $request->title,
            'slug' => Str::slug($request->title), // Use Laravel helper for better slug generation
            'featured_image' => $path, // Store the path/filename in the database
            'content' => $request->content,
            'category_id' => $request->category_id,
            'user_id' => Auth::id()
        ]);

        // 4. Handle tags attachment separately (assuming you have a relationship setup)
        if ($request->tags) {
            $post->tags()->attach($request->tags);
        }

        return redirect()->route('posts')->with('success', 'Post Created Successfully.');
    }
}

Conclusion

Troubleshooting large file uploads in Laravel is a layered process. Never assume the issue lies solely within your application code. First, address the fundamental server constraints by correctly configuring php.ini. Second, adopt modern Laravel practices by utilizing the Storage facade for file management instead of raw filesystem operations. By combining robust server settings with well-structured controller logic, you can ensure that your CMS handles massive files efficiently and reliably. For deeper dives into architectural patterns, exploring resources from the laravelcompany.com documentation is highly recommended.