AskHandle

AskHandle Blog

How to Handle File Uploads in PHP

September 4, 2025Dustin Collins3 min read

How to Handle File Uploads in PHP

Are you interested in managing file uploads in PHP? This guide will help you understand the process of handling file uploads in a straightforward way.

Understanding the Basics

When a user uploads a file through a form, it is sent to the server as part of the HTTP request. PHP uses a superglobal variable called $_FILES to access uploaded file information.

Setting Up the HTML Form

You need to create an HTML form that allows users to select and upload files. Here is a simple example:

html
1<form action="upload.php" method="post" enctype="multipart/form-data">
2    <input type="file" name="file">
3    <input type="submit" value="Upload">
4</form>

In this form, the enctype attribute is set to multipart/form-data, which is necessary for file uploads. The input element with type="file" enables file selection.

Handling the File Upload in PHP

After the user submits the form, the uploaded file data will be in the $_FILES superglobal array. To process the file upload, you can move the uploaded file to a designated directory on the server. Here is an example of PHP code that handles file uploads:

php
1<?php
2if ($_SERVER["REQUEST_METHOD"] == "POST") {
3    $file = $_FILES["file"];
4
5    $fileName = $file["name"];
6    $fileTmp = $file["tmp_name"];
7    $fileSize = $file["size"];
8    $fileError = $file["error"];
9
10    $destination = "uploads/" . $fileName;
11
12    if ($fileError === 0) {
13        if (move_uploaded_file($fileTmp, $destination)) {
14            echo "File uploaded successfully.";
15        } else {
16            echo "Error uploading file.";
17        }
18    } else {
19        echo "There was an error uploading your file.";
20    }
21}
22?>

In this script, we check if the HTTP request method is POST. We access the file details using the $_FILES array and define the destination directory. We handle any upload errors and move the file using move_uploaded_file.

Validating File Uploads

It's important to validate uploads to ensure that only permitted file types and sizes are accepted. You can retrieve file information from the $_FILES array for validation. Here’s an example:

php
1$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
2$maxFileSize = 5 * 1024 * 1024; // 5MB
3
4if (in_array(pathinfo($fileName, PATHINFO_EXTENSION), $allowedExtensions)) {
5    if ($fileSize <= $maxFileSize) {
6        // File is valid, proceed with uploading
7    } else {
8        echo "File is too large. Maximum file size is 5MB.";
9    }
10} else {
11    echo "Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.";
12}

This code defines allowed file extensions and sets a maximum file size limit. It checks whether the uploaded file meets the criteria before proceeding.

Handling Multiple File Uploads

To allow users to upload multiple files, use the multiple attribute in the HTML input element. Here’s an example of an HTML form for multiple uploads:

html
1<form action="upload.php" method="post" enctype="multipart/form-data">
2    <input type="file" name="files[]" multiple>
3    <input type="submit" value="Upload">
4</form>

In the PHP script, loop through the $_FILES array to handle each file upload.

Managing File Upload Errors

Error handling is vital when processing file uploads. The $_FILES array contains an error key that indicates if any errors occurred. Here are some common upload errors and their codes:

  • UPLOAD_ERR_INI_SIZE: The uploaded file exceeds the upload_max_filesize directive in php.ini.
  • UPLOAD_ERR_FORM_SIZE: The uploaded file exceeds the MAX_FILE_SIZE directive in the HTML form.
  • UPLOAD_ERR_PARTIAL: The file was only partially uploaded.
  • UPLOAD_ERR_NO_FILE: No file was uploaded.
  • UPLOAD_ERR_NO_TMP_DIR: Missing a temporary folder.
  • UPLOAD_ERR_CANT_WRITE: Failed to write file to disk.

By checking the error code, you can provide informative messages to users about upload issues.

Handling file uploads in PHP is essential for many web applications. By following the steps presented in this guide, you can effectively manage file uploads while ensuring server security and integrity. Remember to validate uploads and handle errors to provide a smooth user experience.