Return to Snippet

Revision: 39289
at January 16, 2011 08:57 by FatFolderDesigner


Updated Code
/* HTML 5 form */
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" multiple="multiple" name="file[]" /><input type="submit" value="Upload" />
</form>

/* PHP code */
for($i=0;$i<count($_FILES['file']['size']);$i++){
	if(strstr($_FILES['file']['type'][$i], 'image')!==false){
		$file = 'uploads/'.time().' - '.$_FILES['file']['name'][$i];
		move_uploaded_file($_FILES['file']['tmp_name'][$i],$file);
		echo'<a href="'.$file.'"><img src="'.$file.'" style="max-height: 250px;max-width: 500px;" alt="Uploaded Image '.$i.'" /></a><br/>';
	}
}

Revision: 39288
at January 16, 2011 08:49 by FatFolderDesigner


Initial Code
/* HTML 5 form */
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" multiple="multiple" name="file[]" /><input type="submit" value="Upload" />
</form>

/* PHP code */

for($i=0;$i<count($_FILES['file']['size']);$i++){
	if(strstr($_FILES['file']['type'][$i], 'image')!==false){
		$file = 'uploads/'.time().' - '.$_FILES['file']['name'][$i];
		move_uploaded_file($_FILES['file']['tmp_name'][$i],$file);
		echo'<a href="'.$file.'"><img src="'.$file.'" style="max-height: 250px;max-width: 500px;" alt="Uploaded Image '.$i.'" /></a><br/>';
	}
}

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

Initial Description
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.

Initial Title
PHP HTML5 multi-file upload

Initial Tags
php, file, html5

Initial Language
PHP