Upload Multiple Files

PHP Programming

PHP Programming Examples

<html>
	<head>
 		<title>Upload Multiple Files</title>
	</head>
	<body>
	
		/* Encoded form data for uploading a file */
		<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
		
			/* Array of multiple files */
 			<input type="file" name="up_file[]" id="up_file" multiple />
 			<input type="submit" value="upload" />
 		</form>
 		
 		<?php
 			if(isset($_FILES['up_file'])) {
 				
 				/* Iterate file array using 'foreach' */
				foreach($_FILES['up_file']['error'] as $key => $error) {
					
					/* Checking error in files */ 
					if ($error == UPLOAD_ERR_OK) {
						echo 'Name '.$_FILES['up_file']['name'][$key]."<br />";
						echo 'Tmp_name '.$_FILES['up_file']['tmp_name'][$key]."<br />";
						echo 'Size '.$_FILES['up_file']['size'][$key]."<br />";
						echo 'Type '.$_FILES['up_file']['type'][$key]."<br />";
						echo 'Error '.$_FILES['up_file']['error'][$key]."<br />";
						
						/* Upload file if it's a images */ 
						if($_FILES['up_file']['type'][$key] == "image/jpeg" ||
						    $_FILES['up_file']['type'][$key] == "image/png" ||
						    $_FILES['up_file']['type'][$key] == "image/gif" ) {
							$source = $_FILES['up_file']['tmp_name'][$key];
							$target = "upload/".$_FILES['up_file']['name'][$key];
					
							/* Moves an uploaded file to a new location */ 
							if(move_uploaded_file($source, $target)) {
								$size = getImageSize( $target );
								echo '<img width="'.$size[0].'" height="'.$size[1].'" src="'.$target.'" alt="uploaded image" /><br />';
								echo "Image Uploaded Successfully!";
							}
							else
								echo "Fail to upload!";
						}
						else
							echo "It's not a images!";
					}
					else
						echo "Error in File ".$key."<br />";
					
					echo '<br /><br />';
				}
			}
 		?>
 	</body>
</html>


          /****** Output ******/
          

Input:

Choose multiple file image file


Name: order15.png

Size: 197978 bytes

Temp name: /tmp/phpDAGiyf

Type: image/png

Error: 0


Image Uploaded Successfully!



Name: order55.png

Size: 17978 bytes

Temp name: /tmp/phpjFyyrk

Type: image/jpeg

Error: 0


Image Uploaded Successfully!



Name: order15.png

Size: 297978 bytes

Temp name: /tmp/php9WUBnp

Type: image/png

Error: 0


Image Uploaded Successfully!

Learn more about Uploading Multiple Files