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!

8 Years
Discussion Span
Last Post by servis
 	// 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>';
 			// 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

 			//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>';

  			//we will give an unique name, for example the time in unix time format
 			//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)
 		 	$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>';
 				// the new thumbnail image will be placed in images/thumbs/ folder
 				// 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
  //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.'">';

 	<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" >

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.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.