HTML5 Upload Folder With Webkitdirectory
Something that I have always found myself lusting after is the ability to just drag and drop folders into a web app to upload them. So uploading multiple files was an awesome improvement and great fun to play with.
But now, I present to you, webkitdirectory!
HTML5 Folder Upload with webkitdirectory from Alan Layt on Vimeo.
Note:
This will only run on an up-to-date webkit browser. webkitdirectory
is currently non-standard is is purely for playing with. I do not advise you deploy this in a live application.
Update:
I have expanded on folder upload in my Keep Directory Structure When Uploading post.
Enjoy!
I had the pleasure of having a little play around with this when I should have probably been working with processing for university but I do like to procrastinate.
So, without further ado;
The HTML!
<input type="file" name="file_input[]" id="file_input" multiple webkitdirectory="">
Simple, right?
Indeed it is! On to the PHP.
The PHP!
if($_FILES['file_input']){ $uploads = UpFilesTOObj($_FILES['file_input']); $fileUploader=new FileUploader($uploads); } class FileUploader{ public function __construct($uploads,$uploadDir='uploads/'){ foreach($uploads as $current) { $this->uploadFile=$uploadDir.$current->name.".".get_file_extension($current->name); if($this->upload($current,$this->uploadFile)){ echo "Successfully uploaded ".$current->name."n"; } } } public function upload($current,$uploadFile){ if(move_uploaded_file($current->tmp_name,$uploadFile)){ return true; } } } function UpFilesTOObj($fileArr){ foreach($fileArr['name'] as $keyee => $info) { $uploads[$keyee]->name=$fileArr['name'][$keyee]; $uploads[$keyee]->type=$fileArr['type'][$keyee]; $uploads[$keyee]->tmp_name=$fileArr['tmp_name'][$keyee]; $uploads[$keyee]->error=$fileArr['error'][$keyee]; } return $uploads; } function get_file_extension($file_name) { return substr(strrchr($file_name,'.'),1); }
I’ve also been working on a MooTools script to upload the file asynchronously. So stay tuned.
Any comments or suggestions and I’d be delighted to hear them!
Good luck.
Source
Pingback: Upload a Folder Exactly with PHP Ajax with Progress
Pingback: Upload a Folder Exactly with PHP Ajax with Progress | DIGG LINK
Hi Alan,
very nice post. I’ve started today learning PHP and because i need some file uploading i thought your example is will give me a good start. I got your code working but and it works for small stuff ( 5-6 pictures), but it gives me an error (Notice: Undefined index: file_input in C:xampphtdocsfileuploaderajUp.php on line 2) when i try uploading movies or more pictures. Do you know why and how can i have this fix.
Thank you ,
Dan
sorry for my bad english, i’m not native speaker and also very tired today
You are probably also missing an error saying something like:
Warning: POST Content-Length of 578119121 bytes exceeds the limit of 8388608 bytes in Unknown on line 0
While learning, it’s a good idea to have all errors reported. A simple way to achieve this is to add
error_reporting(-1);
near the start of your PHP file.This is because PHP by default does not allow uploading of such large files.
If you locate your php.ini file and set
post_max_size
to2147483647
, this will allow you to upload files up to 2GB.I probably wouldn’t recommend this method if you’re planning on making a live service. (Would be fine for a little local server)
From what I remember, there are methods for handling this using javascript, but are more complicated. (I’ve been planning to do some research on the subject and write a post but things keep getting in the way)
I have a system on my network which uses similarly large files and for the moment I have just been using FTP to upload the files to a temporary folder to wait to be processed with PHP.
Hope this helped.
If you have any other problems, I’d be delighted to lend a hand. ^_^
Thanks for help.Your perfectly right ,i had to change the php.ini file, but also to make a .htaccess file an set:
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 200
php_value max_input_time 200
http://roshanbh.com.np/2008/01/uploading-larger-files-in-php.html
I’m not going to use the example that i used today as a live service.I used this example only for learning purposes, and just to let my friend share some pictures with me without using Skype, Messenger or other fancy file shareing sites.
Thanks again , and hope to see some other great posts from you in the future. Good night.
i Download this code but this is not work as you seen in the video.
can you, Please give me correct code of this program?
The previous code works, but the download for the November update does not include any javascript files, there is no in the php file, and the ajUp.php is an empty file.
If this code works for what Im trying to do, it would be a real time saver.
Thanks for pointing this out. I’ve sorted out the download – this was never a javascript demo though, so I’m not sure why the folder was named to suggest it was. I must have been having a weak brain day.