PHP File Upload Not Working

PHP file upload not working? Check the common reasons behind errors and easy fixes to get your file upload working again.

PHP file upload not working

File upload is one of the most common features in web applications. Whether you are building a user profile system, document submission form, or an image gallery, you will eventually need to upload files using PHP.

But many developers face a frustrating problem — the PHP file upload simply does not work. The file does not upload, the folder stays empty, or sometimes the form submits but nothing happens.

If you searched for "PHP file upload not working", you are probably facing one of these situations:

  • The uploaded file is not appearing in the target folder
  • $_FILES array is empty
  • No error message is shown
  • The upload works on localhost but fails on the server

The good news is that most PHP upload issues are caused by a few common mistakes. In this guide, we will walk through the real reasons why PHP file uploads fail and how to fix them step-by-step.

This guide is written in very simple language so that beginners and experienced developers both can quickly identify and solve the issue.

How PHP File Upload Works (Quick Overview)

Before troubleshooting, it is important to understand how file upload works in PHP.

A typical upload flow looks like this:

  1. User selects a file in an HTML form
  2. The form sends the file using POST request
  3. PHP receives the file in the $_FILES array
  4. The file is moved to a folder using move_uploaded_file()

If any step in this process fails, the upload will not work.

Basic Working Example of PHP File Upload

First, let's look at a simple working example.

HTML Form


<form action="upload.php" method="POST" enctype="multipart/form-data">
    <input type="file" name="myfile">
    <button type="submit">Upload File</button>
</form>

PHP Upload Script


<?php

$targetFolder = "uploads/";
$fileName = $_FILES['myfile']['name'];

move_uploaded_file($_FILES['myfile']['tmp_name'], $targetFolder . $fileName);

echo "File uploaded successfully.";

?>

If this basic setup does not work, then one of the issues below is likely causing the problem.

1. Missing enctype in the Form (Most Common Problem)

This is the number one reason why file upload does not work.

If your form does not include the correct enctype, PHP will not receive the file.

Wrong example:


<form method="POST">

Correct example:


<form method="POST" enctype="multipart/form-data">

The multipart/form-data encoding tells the browser that the form contains file data.

Without it, $_FILES will be empty.

2. Checking the Wrong Superglobal Variable

Many beginners try to access uploaded files using $_POST.

But uploaded files are stored in the $_FILES array.

Wrong way:


$_POST['myfile']

Correct way:


$_FILES['myfile']

The $_FILES array contains information like:

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

3. The Upload Folder Does Not Exist

If the destination folder does not exist, PHP cannot move the uploaded file.

Example:


$targetFolder = "uploads/";

If the uploads folder is missing, the upload will fail silently.

Always make sure the folder exists.

You can create it manually or programmatically.

4. Folder Permission Problem

Even if the folder exists, PHP may not have permission to write files.

This is very common on shared hosting servers.

The upload folder should have write permission.

Typical permission setting:


chmod 755 uploads

If the upload still fails, you may temporarily test with:


chmod 777 uploads

But do not keep 777 permission permanently because it can cause security risks.

5. File Size Limit in PHP Configuration

PHP has default limits for file uploads.

If the file size exceeds the limit, the upload will fail.

Important settings inside php.ini:


upload_max_filesize = 2M
post_max_size = 8M

If you are uploading larger files (like images or PDFs), increase these values.


upload_max_filesize = 20M
post_max_size = 25M

After updating php.ini, restart the web server.

6. Not Using move_uploaded_file()

Uploaded files are stored temporarily on the server.

If you do not move them to a permanent location, they will disappear.

Always use:


move_uploaded_file()

Example:


move_uploaded_file($_FILES['myfile']['tmp_name'], "uploads/" . $_FILES['myfile']['name']);

7. Checking PHP Upload Error Codes

PHP provides error codes for file upload failures.

You can check them using:


$_FILES['myfile']['error']

Example debugging code:


if($_FILES['myfile']['error'] != 0){
    echo "Upload error code: " . $_FILES['myfile']['error'];
}

This will help you identify the exact reason for the failure.

8. File Upload Disabled in PHP

Sometimes file uploads are disabled in server configuration.

Check this setting in php.ini:


file_uploads = On

If it is set to Off, PHP will reject all uploads.

9. Server Security Blocking Upload

Some hosting providers block certain file types for security reasons.

Commonly blocked files:

  • .php
  • .exe
  • .sh

If you are trying to upload these files, the server may reject them automatically.

Best Practice: Secure File Upload Example

Here is a better and safer upload example.


<?php

$uploadDir = "uploads/";

$fileName = basename($_FILES["myfile"]["name"]);

$targetFile = $uploadDir . $fileName;

if(move_uploaded_file($_FILES["myfile"]["tmp_name"], $targetFile)){

    echo "File uploaded successfully";

}else{

    echo "Upload failed";

}

?>

You should also validate:

  • File type
  • File size
  • File extension

This prevents security issues.

Quick PHP File Upload Troubleshooting Checklist

If your PHP file upload is not working, check these things quickly:

  • Form has enctype="multipart/form-data"
  • Form method is POST
  • Using $_FILES not $_POST
  • Upload folder exists
  • Folder has write permission
  • php.ini file size limit is sufficient
  • move_uploaded_file() is used
  • No upload error code returned

In most cases, the issue will be solved by checking these points.

Final Thoughts

PHP file upload problems are very common, especially for beginners. But the good part is that the issue is usually simple.

Most upload failures happen because of:

  • Missing enctype
  • Wrong variable usage
  • Folder permission issues
  • PHP configuration limits

Once you understand how the upload flow works, debugging becomes much easier.

If you are building production applications, always remember to add proper validation, file type restrictions, and security checks to protect your server.

With these fixes, your PHP file upload should start working correctly.

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.