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!

Dani AI

Generated

In this thread asked for a simple PHP form that uploads an image to a directory and then provides the image address for display or copying. The basic flow is straightforward: an HTML form with enctype="multipart/form-data", server-side checks (error code, size and type), move the uploaded file into a safe uploads folder, store the saved path (or URL) and then output that path or render an image tag. The replies already show the general idea, but a few important gaps and hardening steps are worth adding.

’s example demonstrates the common flow (extension check, image-size check, thumbnail creation). It’s a good starting point but should be improved before use on any public site. Specifically: do not trust the client filename or extension alone; verify image contents with server-side checks such as finfo_file or getimagesize; check $_FILES[...]['error'] and is_uploaded_file; and use move_uploaded_file for the final save rather than generic file copies. Also generate a safe, unique filename (avoid predictable names) and ensure the upload directory has correct ownership and restrictive permissions (avoid world-writable 777).

Security and operational notes: limit accepted MIME types to known image types (JPEG, PNG, GIF, WebP), enforce file-size and pixel-dimension limits, strip or ignore embedded scripts/metadata where needed, and escape any path printed into HTML (use htmlspecialchars) to prevent XSS. Consider storing uploads outside the webroot or using server rules to prevent execution of uploaded files. When making thumbnails, use GD or Imagick and watch memory_limit for large images.

Quick checklist:

Recommended Answers

All 2 Replies

$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???

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.