Pure Javascript, HTML and PHP without any library
This demo allows you to choose a folder via a standard HTML file input. All files (including nested files) inside that folder are added to the input's files list. This script using webkitdirectory
attribute that compatible with browser Chrome, Edge or Firefox.
Choose a folder, and all files in that folder will be uploaded recursively (Inspiration came from Google Drive).
Supported browser: Chrome, Edge, Firefox, Android, Samsung and other browsers that support webkitdirectory
attribut.
- Download or clone this repo
- Put the script on your web server's document root
- Open that folder in browser, eg: http://yourweb/uploadfolder/
- HTML file that contain input element and user interfaces.
- CSS file for styling.
- Javascript file for handling file input and send request to server.
- PHP file that accept file from client and save file in server.
<h3>Choose Folder</h3>
<div class="picker"><input type="file" id="picker" webkitdirectory multiple></div>
...
<script src="main.js"></script>
// On button input change (picker), process it
picker.addEventListener('change', e => {
...
// Process every single file
for (var i = 0; i < picker.files.length; i++) {
var file = picker.files[i];
sendFile(file, file.webkitRelativePath);
}
});
// Function to send a file, call PHP backend
sendFile = function(file, path) {
...
// Set post variables
formData.set('file', file); // One object file
formData.set('path', path); // String of local file's path
// Do request
request.open("POST", 'process.php');
request.send(formData);
};
<?php
require_once("include/upload.class.php");
$up = new Upload();
$up->set_folder("upload");
$up->process($_POST["path"], $_FILES["file"]);
?>
Progress bar- Configurable Javascript
- Composer style PHP class