Hi! I'm trying to find a basic PHP code that uses an HTML form to upload an image to a specified directory and after that the image address can be called for displaying the image with PHP or putting it in a textarea box for linking. I know it seems simple but I've been looking everywhere for one. I must be looking in the wrong places. If anyone can give any help I would so appreciate it!

Thank you!

$image=$_FILES['broad1_image']['name'];
 	// if it is not empty
 	if ($image) 
 	{
 		// get the original name of the file from the clients machine
 		$filename = stripslashes($_FILES['broad1_image']['name']);
 		
 		// get the extension of the file in a lower case format
 	 	$extension = getExtension($filename);
 		$extension = strtolower($extension);
 		// if it is not a known extension, we will suppose it is an error, print an error message 
 		//and will not upload the file, otherwise we continue
 		if (($extension != "jpg")  && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif"))	
 		{
 			echo '<h1>Unknown extension!</h1>';
 			$errors=1;
 		}
 		else
 		{
 			// get the size of the image in bytes
 			// $_FILES[\'image\'][\'tmp_name\'] is the temporary filename of the file in which 
			//the uploaded file was stored on the server
 			$size=getimagesize($_FILES['broad1_image']['tmp_name']);
 			$sizekb=filesize($_FILES['broad1_image']['tmp_name']);

 			//compare the size with the maxim size we defined and print error if bigger
 			if ($sizekb > MAX_SIZE*1024)
 			{
 				echo '<h1>You have exceeded the size limit!</h1>';
 				$errors=1;
 			}


  			//we will give an unique name, for example the time in unix time format
 			$image_name=image1.'.'.$extension;
 			//the new name will be containing the full path where will be stored change these to the file where you would like to store the images.  (images folder)
 		 	$broad1name="image/".$image_name;
 		 	$broad1name2="image/thumbs/thumb_".$image_name . $rand;
 			$copied = copy($_FILES['broad1_image']['tmp_name'], $broad1name);
 			$copied = copy($_FILES['broad1_image']['tmp_name'], $broad1name2);
 			//we verify if the image has been uploaded, and print error instead
 			if (!$copied) {
 				echo '<h1>Copy unsuccessfull!</h1>';
 				$errors=1;
 			}
 			else
 			{
 				// the new thumbnail image will be placed in images/thumbs/ folder
 				$thumb_name=$broad1name2;
 				// call the function that will create the thumbnail. The function will get as parameters 
 				//the image name, the thumbnail name and the width and height desired for the thumbnail
 				$thumb=make_thumb($broad1name,$thumb_name,WIDTH,HEIGHT);
 			}
 		}	
 	}
 }
			
  //If no errors registred, print the success message and show the thumbnail image created
 if(isset($_POST['Submit']) && !$errors) 
 {
 	echo "<h5>Thumbnail created Successfully!</h5>";
 	echo '<img src="'.$thumb_name.'">';
 } 

 ?> 
</p>
 	<form name="newad" method="post" enctype="multipart/form-data"  action="">
		<input type="file" name="broad1_image"  >
		<input name="Submit" type="submit" id="image1" value="Upload image" >
	
</form>

that should do the job for you. i am using this on a website i am building

if you are requiring a complete well documented and very much easy tutorial then follow the link www.php-mysql-tutorial.com. where everything about image upload is discussed in image gallery link..and plz let us confirm....whether it is enough???

This article has been dead for over six months. Start a new discussion instead.