PHP File Upload Errors Fix

From UPLOAD_ERR_INI_SIZE to UPLOAD_ERR_NO_TMP_DIR. Understand what each error code means and exactly how to fix them in php.ini.

PHP File Upload Errors

File upload is a very common feature in PHP applications. Developers use it to upload profile images, documents, CSV files, PDFs, and many other types of files.

But during development, many programmers face a confusing situation — the file upload fails and PHP returns an error.

You may see something like this while debugging:


$_FILES['file']['error'] = 1

Or maybe:


$_FILES['file']['error'] = 3

At first, these numbers look strange. Many developers do not know what these error codes actually mean.

If you searched for "PHP file upload errors", you probably want to understand:

  • What each PHP upload error code means
  • Why file uploads fail
  • How to fix upload problems quickly
  • How to debug upload errors in real applications

In this guide, we will explain all PHP file upload error codes in very simple language. We will also show practical solutions so you can fix the problem fast.

This article is written for both beginners and experienced developers who want to debug PHP upload problems easily.

Where PHP File Upload Errors Appear

When a user uploads a file using an HTML form, PHP stores upload information inside the $_FILES array.

This array contains important data such as:

  • File name
  • Temporary file path
  • File size
  • Upload error code

Example:


$_FILES['myfile']['name']
$_FILES['myfile']['tmp_name']
$_FILES['myfile']['size']
$_FILES['myfile']['error']

The error index tells us whether the upload succeeded or failed.

PHP File Upload Error Codes List

PHP defines several constants for upload errors.

Here are the most important ones:

  • UPLOAD_ERR_OK
  • UPLOAD_ERR_INI_SIZE
  • UPLOAD_ERR_FORM_SIZE
  • UPLOAD_ERR_PARTIAL
  • UPLOAD_ERR_NO_FILE
  • UPLOAD_ERR_NO_TMP_DIR
  • UPLOAD_ERR_CANT_WRITE
  • UPLOAD_ERR_EXTENSION

Each of these errors represents a different problem during the upload process.

Let's understand them one by one.

Error Code 0 – UPLOAD_ERR_OK

This means the file upload was successful.

No error occurred.


$_FILES['myfile']['error'] = 0

If you see this code but the file is still missing, the problem may be in your PHP script — usually related to move_uploaded_file().

Error Code 1 – UPLOAD_ERR_INI_SIZE

This error means the uploaded file is larger than the allowed limit in the php.ini configuration.

PHP settings:


upload_max_filesize

Example configuration:


upload_max_filesize = 2M

If a user uploads a file larger than this limit, PHP will reject the upload.

Solution

Increase the limit inside php.ini:


upload_max_filesize = 20M
post_max_size = 25M

After updating the file, restart the web server.

Error Code 2 – UPLOAD_ERR_FORM_SIZE

This error occurs when the uploaded file exceeds the size defined in the HTML form.

Some developers add a hidden field like this:


<input type="hidden" name="MAX_FILE_SIZE" value="2000000">

If the uploaded file is larger than this value, the upload will fail.

Solution

Increase the MAX_FILE_SIZE value or remove the restriction if it is not needed.

Error Code 3 – UPLOAD_ERR_PARTIAL

This error means the file was only partially uploaded.

Possible reasons:

  • Network interruption
  • Server timeout
  • Browser stopped upload

Solution

This error is usually temporary. Ask the user to upload the file again.

Also check server timeout settings.

Error Code 4 – UPLOAD_ERR_NO_FILE

This means the user did not select any file.


$_FILES['myfile']['error'] = 4

This is not really an error — it simply means no file was uploaded.

Solution

Always check if a file was selected before processing the upload.


if($_FILES['myfile']['error'] == 4){
    echo "Please select a file to upload.";
}

Error Code 6 – UPLOAD_ERR_NO_TMP_DIR

PHP uses a temporary folder to store uploaded files before moving them to the final location.

If this temporary folder is missing, PHP cannot store the file.

Solution

Check your php.ini configuration:


upload_tmp_dir

Set a valid folder path.

Error Code 7 – UPLOAD_ERR_CANT_WRITE

This error means PHP failed to write the uploaded file to disk.

This usually happens because of folder permission issues.

Solution

Make sure the upload directory has write permission.

Example:

chmod 755 uploads

If the problem continues, temporarily test with:


chmod 777 uploads

Error Code 8 – UPLOAD_ERR_EXTENSION

This error happens when a PHP extension stops the upload.

This is rare but can happen if security extensions block certain file types.

Solution

Check installed PHP extensions and security modules.

Sometimes hosting providers block dangerous file types like:

  • .php
  • .exe
  • .sh

How to Display PHP Upload Error Messages

A good practice is to convert upload error codes into readable messages.

Example:


switch($_FILES['myfile']['error']){

case 0:
echo "Upload successful";
break;

case 1:
echo "File exceeds server upload limit";
break;

case 2:
echo "File exceeds form upload limit";
break;

case 3:
echo "File partially uploaded";
break;

case 4:
echo "No file selected";
break;

default:
echo "Unknown upload error";
}

This makes debugging much easier.

Real World Debugging Tip

When file upload fails, always start by printing the entire $_FILES array.


print_r($_FILES);

This simple step helps you quickly identify:

  • Upload error code
  • File size
  • Temporary path

Many developers skip this step and spend hours debugging the wrong problem.

Best Practices for PHP File Upload

For production applications, follow these best practices:

  • Validate file type before upload
  • Limit maximum file size
  • Rename uploaded files
  • Store uploads outside public folders
  • Check upload error codes

These practices help prevent security vulnerabilities and server issues.

Quick Troubleshooting Checklist

If you face PHP file upload errors, check these quickly:

  • Verify php.ini upload size limits
  • Check form enctype="multipart/form-data"
  • Check folder permissions
  • Inspect $_FILES array
  • Verify temporary upload folder
  • Confirm server allows file type

In most cases, these checks will solve the problem.

Final Thoughts

PHP file upload errors can look confusing at first because they appear as simple numbers.

But once you understand what each error code means, debugging becomes much easier.

Most upload issues are related to:

  • File size limits
  • Server configuration
  • Folder permissions
  • Missing file selection

By checking the upload error code and following the troubleshooting steps in this guide, you can quickly fix almost any PHP file upload problem.

If you regularly work with file uploads, it is a good idea to build a proper upload validation system to handle errors gracefully.

Ketan Patel - PHP & MySQL Performance Optimization Specialist
Ketan Patel

PHP & MySQL Performance Optimization Specialist

I specialize in diagnosing and fixing slow PHP applications, optimizing MySQL queries, and resolving backend bottlenecks in live production systems. My approach is metric-driven — identifying root causes through profiling, execution analysis, and structured optimization instead of guesswork.