Skip to content

Instantly share code, notes, and snippets.

@mattpass
Created February 16, 2012 07:43
Show Gist options
  • Save mattpass/1843030 to your computer and use it in GitHub Desktop.
Save mattpass/1843030 to your computer and use it in GitHub Desktop.
Drag & Drop File & Folder Uploader
<?php
echo 'Received by PHP:<br><br>';
for ($i=1;$i<=($_POST['numFiles']*1);$i++) {
echo $_POST['fileName'.$i].'<br>';
if (!strpos($_POST['fileName'.$i],".")) {
echo '[DIR]<br><br>';
} else {
echo $_POST['fileContent'.$i].'<br><br>';
}
}
?>
<html>
<head>
<title>Drag & Drop Multiple File Upload Test</title>
<script>
// Function to cancel default events
var cancelEvent = function(event) {
if(event.preventDefault) {
event.preventDefault();
}
return false;
}
// Event listeners added to cancel out dragover & dragenter events
window.addEventListener('dragover', cancelEvent, false);
window.addEventListener('dragenter', cancelEvent, false);
// Drop event triggers FileReader to receive files
window.addEventListener('drop', function(event) {
var files = event.dataTransfer.files;
for (i=0;i<files.length;i++) {
var newField;
newField = document.createElement('textarea');
newField.id = newField.name = "fileName"+(i+1);
document.getElementById('fileForm').appendChild(newField);
newField = document.createElement('textarea');
newField.id = newField.name = "fileContent"+(i+1);
document.getElementById('fileForm').appendChild(newField);
var file = files[i];
var reader = new FileReader();
reader.onload = (function() {
return function(e) {
window['name'+nextID] = files[nextID-1].name;
window['size'+nextID] = files[nextID-1].size;
window['content'+nextID] = e.target.result;
console.log("Name: "+window['name'+nextID] + ", Size: " + window['size'+nextID] + "bytes");
console.log("Content:" + window['content'+nextID]);
document.getElementById('fileName'+nextID).value = files[nextID-1].name;
document.getElementById('fileContent'+nextID).value = window['content'+nextID];
if (i==nextID) {document.getElementById('fileForm').submit();};
nextID++;
};
})(file);
reader.readAsText(file);
}
document.getElementById('numFiles').value = i;
}, false);
var nextID = 1;
</script>
</head>
<body>
Drag & drop files & folders onto the webpage to have the files upload.<br><br>
Need to improve this to recognise files in folders, subfolders etc.<br><br>
Ideas? Please tweet me: <a href="https://twitter.com/#!/mattpass" target="_blank">@mattpass</a><br><br>
(Note: Files & folders not actually saved on the webspace, PHP only reports what's been uploaded).<br><br>
<form action="upload.php" target="saveFrame" id="fileForm" method="POST">
<input type="text" id="numFiles" name="numFiles" size="3"> = no of files<br>
</form>
<iframe id="save" name="saveFrame" style="height: 400px"></iframe>
</body>
</html>
@mattpass
Copy link
Author

You can drag & drop files & folders onto the webpage to have them posted to the PHP page in the iFrame. However, this only works 1 level deep and doesn't cover whats inside folders. Any ideas on how I can make it also cover files & folders within folders?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment