how to read FormData object in php

how to read FormData object in php

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

Stefan Bogdanescu

Founder & Senior Architect

Decoding FormData in PHP: Handling AJAX File Uploads

When building a web application that handles file uploads via AJAX, you quickly encounter a specific behavior of the FormData interface. Your JavaScript code successfully bundles the file into a request object, but the standard $input['file'] access pattern often fails when using jQuery's $.ajax. This isn't because PHP is ignoring the data; it is because the default configuration sends the payload as raw text rather than multipart form data.

To read the file content in your PHP backend, you must explicitly handle the request headers and ensure the server interprets the incoming stream correctly. The core issue lies in the JavaScript configuration where contentType is set to false and processData is set to false. These flags tell jQuery not to transform the data into a standard query string or form array. Instead, it sends the raw bytes of the FormData object directly to the server.

In PHP, when you receive a request with Content-Type: multipart/form-data, the framework automatically parses the body into the $_FILES superglobal. This is distinct from $_POST. If your PHP script attempts to access $request->get('file') or $_POST['file'], it will return null because the data never reached the standard POST parsing stage.

The correct approach involves accessing the uploaded file through the $_FILES array. The structure of this array allows you to extract the temporary location of the file, its original name, and its MIME type. Here is a practical example of how to retrieve and handle the file in process.php:

<?php
// Verify that the upload was successful
if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) {
    die("No file uploaded or an error occurred.");
}

// Define a safe destination for the uploaded file
$destination = 'uploads/' . basename($_FILES['file']['name']);

// Ensure the upload directory exists and is writable
if (!is_dir('uploads')) {
    mkdir('uploads', 0755, true);
}

// Move the uploaded file from the temporary location to your destination
if (move_uploaded_file($_FILES['file']['tmp_name'], $destination)) {
    echo "File uploaded successfully: " . $destination;
} else {
    echo "Failed to move uploaded file.";
}

This code block demonstrates the standard pattern for handling AJAX uploads. The key function here is move_uploaded_file(). It verifies that the file was indeed a valid upload and moves it from the system's temporary directory (usually /tmp) to your intended storage location. This step is critical for security and memory management, as leaving files in temporary directories can lead to disk space issues or security vulnerabilities if the server restarts.

It is also worth noting that you should sanitize filenames before saving them. An attacker could potentially upload a file with a malicious extension or path traversal characters like ../../. Using basename() helps prevent directory traversal attacks by stripping any directory components from the filename, ensuring the file is only saved within the intended folder.

If you are migrating away from raw PHP scripts or building on top of a framework like Laravel, the underlying principle remains similar but the syntax shifts to using request objects that map $_FILES data into convenient properties. Understanding this distinction between standard POST requests and raw FormData streams is essential for robust file handling in modern web applications. For developers looking to streamline their backend logic further, frameworks often provide helpers that abstract these details, reducing the boilerplate code required to handle uploads securely.

Our network