PHP HTML5 multi-file upload


/ Published in: PHP
Save to your folder(s)

Uploading multiple files at once used to be strictly in the realm of flash or required javascript to crate multiple upload forms as needed. The first method has the problems with the general incompatibility of flash. The second, well, it's a very low tech method and limits you to selecting only one file per upload form, real pain.

First is the HTML form. Pretty standard all you gotta do is add the multiple tag and add brakets to the variable name. Those brakets are the key to the next set, they tell PHP to make the variable an array.

Then is the PHP code. First thing you have to do is get the number of files, I used the count() on an variable then run through till you hit the count, using the variable in each $_FIES call. It's easy to retrofit an older system this way.

An example of the above code can be found at the link.


Copy this code and paste it in your HTML
  1. /* HTML 5 form */
  2. <form action="upload.php" method="post" enctype="multipart/form-data">
  3. <input type="file" multiple="multiple" name="file[]" /><input type="submit" value="Upload" />
  4. </form>
  5.  
  6. /* PHP code */
  7. for($i=0;$i<count($_FILES['file']['size']);$i++){
  8. if(strstr($_FILES['file']['type'][$i], 'image')!==false){
  9. $file = 'uploads/'.time().' - '.$_FILES['file']['name'][$i];
  10. move_uploaded_file($_FILES['file']['tmp_name'][$i],$file);
  11. echo'<a href="'.$file.'"><img src="'.$file.'" style="max-height: 250px;max-width: 500px;" alt="Uploaded Image '.$i.'" /></a><br/>';
  12. }
  13. }

URL: http://fatfolderdesign.com/167/unimportant/html5-mulit-upload

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.