Is it possible to upload a Folder using HTML Form / PHP script

Is it possible to upload a Folder using HTML Form / PHP script

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

Stefan Bogdanescu

Founder & Senior Architect

Is it possible to upload a Folder using HTML Form / PHP script

Short answer: no. You cannot directly upload an entire folder structure from a client's file system into your server via a standard HTML <form> and PHP script. The reason is fundamental to how browsers and servers interact with the file system. A browser operates in a sandboxed environment; it has no visibility into the user's local directory tree, nor does it possess the ability to package arbitrary subdirectories and their contents into a single stream for transmission.

When you select files using an <input type="file" multiple> element, the browser creates a virtual filesystem object containing only the selected paths relative to the current directory. It cannot traverse upward to parent folders or include sibling directories unless they are explicitly selected by the user. Even if you could somehow read the local folder structure, transmitting every file individually with unique filenames while preserving the exact directory hierarchy requires specific handling that standard form submissions do not support out of the box.

The Mechanics of File Uploads

To understand why a direct folder upload fails, look at how PHP handles $_FILES. When a user selects multiple files in a single input field, PHP wraps them in an array under the key specified by your name attribute. However, each file is treated as an independent entity. The browser does not create a "folder" object on the client side that you can zip and send; it simply lists the selected paths.

If your goal is to replicate the behavior of uploading a folder—preserving the directory structure like 2014/February/image1.jpg and 2014/February/image2.jpg—you must construct this structure on the server side after the files arrive. You cannot rely on the form submission to carry the folder metadata.

Implementing Folder Structure Uploads

There are two primary approaches to solving this: using a third-party library like HTML5 Drag & Drop API combined with JavaScript to handle bulk uploads, or implementing a multi-file upload script that reconstructs the directory tree.

The most robust native solution involves allowing users to select multiple files and then iterating through them in PHP to recreate the folder paths. Here is how you can modify a standard upload handler:

<?php
$maxUploadSize = 10 * 1024 * 1024; // 10MB limit

if (isset($_FILES['images']) && is_array($_FILES['images']['name'])) {
    foreach ($_FILES['images'] as $file) {
        if ($file['error'] === UPLOAD_ERR_OK) {
            $targetPath = 'content/2014/February/' . basename($file['name']);
            
            // Check if the target path already exists
            if (!file_exists(dirname($targetPath))) {
                // Create the directory structure if it doesn't exist
                mkdir(dirname($targetPath), 0755, true);
            }

            move_uploaded_file($file['tmp_name'], $targetPath);
        }
    }
}
?>

In this example, we assume the user has selected multiple files via an input with multiple attribute. The PHP script iterates through the uploaded array and explicitly creates the directory using mkdir() before moving the file. This ensures that even if a user selects folder/subfile.jpg, the logic can be adjusted to parse the original filename's path or force a flat structure, depending on your requirements.

Trade-offs and Limitations

A critical limitation of this approach is security. If you attempt to allow users to specify arbitrary paths (e.g., uploading directly to /etc/), you risk directory traversal attacks. Always sanitize file names using basename() and restrict uploads to a specific, writable directory like content/. Additionally, PHP's default configuration often limits the number of simultaneous uploads or the total size based on post_max_size and upload_max_filesize.

Another consideration is performance. Uploading hundreds of small images via a single form submission can hit browser timeout limits or server memory constraints (max_execution_time). For large batches, consider chunked uploads or asynchronous JavaScript requests (AJAX) to handle progress updates without freezing the interface.

Conclusion

While you cannot magically upload a folder in one click using standard HTML forms, reconstructing the directory structure on the server is entirely feasible and secure if implemented correctly. By leveraging PHP's file manipulation functions to create subdirectories dynamically, you achieve the same visual result as uploading a folder, without requiring complex client-side tools or proprietary libraries.

Our network