Verifying the type of image (ie. the extension)

A

Anonymous

Guest
Here is what I have to verify the image extension. I just want to make sure the file that is being uploaded in a .jpg image and not anything else. Only jpegs.

Code:
if ( ($image) && ($image != "") ) {
   
   	$type = basename($image_type);
	switch ($type) {
		case "jpeg":
		case "jpg":	$filename = "Images/$prodname.jpg";
					copy ($image, $filename);
					$sql = "update ctlg_products
							set image = '$filename'
							where prodname = $prodname";
					$iresult = @mysql_query($sql);
					if (!$iresult) {
						echo "There was a database error when executing <PRE>$sql</PRE>";
						echo mysql_error();
						exit;
					}
					break;
		default:	echo "Invalid picture format: $image_type";
				
	}   
   }
	if (!$result)
		return false;
	else
    	return true; 
}

Remember also that $image contains the path to the image on their computer. Came from a form. When I use this it always says it's not a jpeg file even when it is.

Any hints?
 
Grigory said:
Here is what I have to verify the image extension. I just want to make sure the file that is being uploaded in a .jpg image and not anything else. Only jpegs.

Code:
if ( ($image) && ($image != "") ) {
   
   	$type = basename($image_type);
	switch ($type) {
		case "jpeg":
		case "jpg":	$filename = "Images/$prodname.jpg";
					copy ($image, $filename);
					$sql = "update ctlg_products
							set image = '$filename'
							where prodname = $prodname";
					$iresult = @mysql_query($sql);
					if (!$iresult) {
						echo "There was a database error when executing <PRE>$sql</PRE>";
						echo mysql_error();
						exit;
					}
					break;
		default:	echo "Invalid picture format: $image_type";
				
	}   
   }
	if (!$result)
		return false;
	else
    	return true; 
}

Remember also that $image contains the path to the image on their computer. Came from a form. When I use this it always says it's not a jpeg file even when it is.

Any hints?
Hi!
I'm too long time look at your code...
read man about basename....
As you see in your code $type = basename($image_type), variable $type it's a not extension it's file with extension.
Interstin how you do next?:
Code:
switch ($type) {
  case "jpeg":
  case "jpg":
	$filename = "Images/$prodname.jpg";
 
Back
Top