PHP

How to Fix the “Uploaded File Exceeds the upload_max_filesize Directive in php.ini” Error

“Uploaded File Exceeds the upload_max_filesize Directive in php.ini”

If you have ever tried to upload a file in PHP and were greeted by the message “Uploaded file exceeds the upload_max_filesize directive in php.ini”, you are not alone. This error usually appears at the worst possible moment right before a deadline or during a client demo. The good news is that this error is common, well-understood, and completely fixable.

This error means PHP is blocking your file upload because the file is bigger than what PHP allows. PHP has a built-in size limit for uploads, and when your file goes past that limit, PHP stops the upload to protect the server. Think of it like a bouncer at a club who checks your ID if your file is “too big,” it doesn’t get in.

Where This Error Usually Shows Up

You will often see this error when uploading images, videos, PDFs, ZIP files, or backups. It can appear in WordPress, Laravel, plain PHP scripts, or any CMS that uses PHP behind the scenes. Sometimes it shows as a friendly error message, and sometimes it breaks the page completely.

Why PHP Has Upload Limits in the First Place

PHP upload limits exist for security and performance reasons. Large file uploads can slow down servers, fill up disk space, or even crash shared hosting environments. By setting limits, PHP helps prevent abuse and accidental overloads. The problem starts when the default limits are too small for real-world use.

Understand upload_max_filesize and Its Friends

The upload_max_filesize setting controls the maximum size of a single uploaded file. However, it does not work alone. Another setting called post_max_size also plays a big role. If post_max_size is smaller than upload_max_filesize, your upload will still fail. There is also max_execution_time and memory_limit, which can quietly cause issues during large uploads.

Checking Your Current PHP Upload Limits

Before fixing anything, you should check your current PHP settings. The easiest way is to create a PHP info file.

<?php
phpinfo();
?>

Upload this file to your server and open it in a browser. Look for upload_max_filesize and post_max_size. This tells you exactly what PHP is allowing right now.

Fix the Error by Editing php.ini

The most reliable way to fix this error is by editing the php.ini file. This file controls PHP’s main configuration. On many servers, you can find it in /etc/php/, /usr/local/lib/, or inside your hosting control panel.

upload_max_filesize = 64M
post_max_size = 64M
max_execution_time = 300
memory_limit = 256M

Save the file and restart your web server. This method works best on VPS or dedicated servers where you have full control.

Fix the Error Using .htaccess

If you are on shared hosting and cannot access php.ini, you can often fix the issue using the .htaccess file.

php_value upload_max_filesize 64M
php_value post_max_size 64M
php_value max_execution_time 300
php_value max_input_time 300

Place this code at the top of your .htaccess file. If your server allows it, this change takes effect immediately.

Fix the Error Using php.ini Overrides

Some hosting providers allow you to create a custom php.ini file inside your project directory. This file overrides the global PHP settings.

upload_max_filesize = 64M
post_max_size = 64M

This approach works well on shared hosting platforms that support local PHP configurations.

Fix the Error in cPanel the Easy Way

If your host uses cPanel, you can fix this error without touching code. Open cPanel, go to “Select PHP Version” or “MultiPHP INI Editor,” and update the values for upload_max_filesize and post_max_size. Save the changes, and you are done.

Fix the Error in WordPress

WordPress users see this error a lot when uploading themes or plugins. While WordPress shows its own upload limits, those limits come directly from PHP. Once you increase the PHP limits using any method above, WordPress will automatically allow larger uploads. You can also add this to wp-config.php for extra safety.

@ini_set('upload_max_filesize', '64M');
@ini_set('post_max_size', '64M');
@ini_set('memory_limit', '256M');

Fix the Error in Laravel Applications

Laravel relies on PHP’s upload limits as well. Once PHP is configured correctly, Laravel uploads work smoothly. However, you should also validate file size in your controller to avoid user confusion.

$request->validate([
    'file' => 'required|file|max:65536'
]);

This ensures users get a clear error message instead of a server failure.

Complete File Upload Example in PHP

Here is a complete and clean PHP upload example that works with larger files once PHP limits are updated.

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    if (isset($_FILES['file'])) {
        $file = $_FILES['file'];
        $uploadDir = 'uploads/';
        $uploadFile = $uploadDir . basename($file['name']);

        if ($file['error'] === 0) {
            if (move_uploaded_file($file['tmp_name'], $uploadFile)) {
                echo "File uploaded successfully!";
            } else {
                echo "Failed to upload file.";
            }
        } else {
            echo "Upload error code: " . $file['error'];
        }
    }
}
?>
<form method="POST" enctype="multipart/form-data">
    <input type="file" name="file" required>
    <button type="submit">Upload</button>
</form>

This example shows how PHP handles file uploads cleanly and safely.

Common Mistakes That Still Cause This Error

Many people increase upload_max_filesize but forget post_max_size. Others update the file but forget to restart Apache or PHP-FPM. Some hosts block .htaccess overrides completely. Double-checking these details saves hours of frustration.

Comparing This Guide With Competitor Content

Most competitor articles only explain one fix, usually editing php.ini, and stop there. Some do not explain why the error happens at all. Others skip real coding examples and leave beginners confused. This guide goes further by explaining the logic behind the error, covering all fix methods, and providing complete upload code examples.

Final Thoughts

The “Uploaded file exceeds the upload_max_filesize directive in php.ini” error looks scary, but it is one of the easiest PHP issues to fix. Once you understand what causes it, you can solve it in minutes instead of hours. Update your PHP limits, test your uploads, and move on with confidence. And yes, you can finally upload that big file without PHP yelling at you again.

author-avatar

About MALIK SHAFI

Experienced PHP Developer with a strong background in building scalable web applications in the IT services sector. Proficient in PHP frameworks like Laravel and CodeIgniter, and front-end technologies, Skilled in MySQL database management, RESTful API integration, and working with AWS services such as EC2 and S3. Extensive hands-on experience with Nginx configuration on AWS EC2 instances, optimizing for performance and security.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments