In 2025, the webkitdirectory attribute of input element is not standard yet, but widely accepted.
With this attribute,
<input type="file" name="file" webkitdirectory />
you can select only directories, not files. Once selected, the browser collects all the files in the directory and subdirectories recursively and change the name of the files automatically to reflect the directory structure.
For example, if you chose the rootdir
rootdir/
- file1.ext
- subdir1/
- file2.ext
- subdir2/
you will get two file objects with
- "rootdir/file1.ext"
- "rootdir/subdir1/file2.ext"
as their names respectively, so that you can easily reconstruct the directory structure from the formData at the server side. Note that empty directories are not selected though.