Get Website Learn Coding Tech Story

PHP Programming Examples

<html>
	<head>
 		<title>Upload File</title>
	</head>
	<body>
	
		/* Encoded form data for uploading a file */
		<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
 			<input type="file" name="up_file" id="up_file" />
 			<input type="submit" value="upload" />
 		</form>
 		
 		<?php
 			if(isset( $_FILES['up_file'])){
				echo "Name: ".     $_FILES['up_file']['name']       ."<br />";
				echo "Size: ".     $_FILES['up_file']['size'] ." bytes<br />";
				echo "Temp name: ".$_FILES['up_file']['tmp_name']   ."<br />";
				echo "Type: ".     $_FILES['up_file']['type']       ."<br />";
				echo "Error: ".    $_FILES['up_file']['error']      ."<br />";

				/* Upload file if it's a images */
				if($_FILES['up_file']['type'] == "image/jpeg" ) {
					$source = $_FILES['up_file']['tmp_name'];
					$target = "upload/".$_FILES['up_file']['name'];
					
					/*  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!";
			}
 		?>
 	</body>
</html>


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

Input:

Choose a image file


Name: order05.jpg

Size: 197978 bytes

Temp name: /tmp/phphWY5Zn

Type: image/jpeg

Error: 0


Image Uploaded Successfully!

Learn more about Uploading Files