How to check whether the user uploaded a file in PHP?

How to check whether the user uploaded a file in PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

How to Check Whether the User Uploaded a File in PHP

Determining if a file upload occurred during form submission is a common requirement, especially when handling optional fields. You might want to process additional logic only if an attachment exists, or conversely, skip validation rules entirely if the user left the input blank. A frequent instinct is to check the size index of the $_FILES superglobal array. However, relying solely on $_FILES['myfile']['size'] <= 0 is fundamentally flawed and can lead to unexpected behavior in your application.

The core issue lies in how PHP initializes the $_FILES array. When a form field does not contain an uploaded file—whether the user omitted it or explicitly chose not to upload anything—the corresponding entry in $_FILES might be completely missing, or it might exist with specific default values depending on your server configuration and the enctype of the form.

If the input field is named correctly and has a value, PHP populates $_FILES. If no file is uploaded, the array key often does not exist at all in standard configurations. Attempting to access $_FILES['myfile']['size'] on a non-existent key triggers a warning and returns null, which can break your script or cause logic errors if you aren't handling the error reporting correctly. Even if the key exists, checking for a size less than or equal to zero is unreliable because valid uploads will always have a positive size, but an empty upload (if allowed by server settings) might report a size of 0.

The most robust way to detect an uploaded file is to check whether the name index is set and not empty. The name field contains the original filename provided by the client. If no file was selected or submitted, this value will be absent or empty. You can use the isset() function combined with a truthy check on the name:

if (isset($_FILES['myfile']['name']) && $_FILES['myfile']['name'] !== '') {
    // A file was actually uploaded and named something
    $originalName = $_FILES['myfile']['name'];
} else {
    // No file was uploaded; skip validation or handle as optional
    return; 
}

This approach is safer than checking size because it directly reflects the user's action of selecting a file. It avoids false positives where a zero-byte file might be interpreted as "no file" when it could technically be a valid (though empty) upload artifact. Additionally, you should always verify that the error index indicates a successful upload before proceeding. The $_FILES['myfile']['error'] constant will contain UPLOAD_ERR_OK (value 0) only if the file was uploaded without errors.

if (isset($_FILES['myfile']) && $_FILES['myfile']['error'] === UPLOAD_ERR_OK) {
    // Proceed with validation
}

When implementing this logic, remember that the presence of $_FILES does not guarantee a valid file. You must also ensure your HTML form has enctype="multipart/form-data". Without this attribute, file uploads are ignored by PHP entirely, and $_FILES will remain empty regardless of user intent.

In frameworks like Laravel, you don't typically inspect raw $_FILES arrays directly; instead, the framework provides helpers that abstract these checks away. However, understanding the underlying mechanics remains crucial when debugging upload issues or writing custom validation logic outside the standard Eloquent model rules. The distinction between "no file selected" and "empty file uploaded" is subtle but important for strict compliance requirements. Always prioritize checking the name field for existence and content before relying on size metrics to determine if an action should be taken.

Our network