Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => Images

Verifying the type of image (ie. the extension)

images php coding issues or problems here.

Moderators: macek, egami, gesf

Verifying the type of image (ie. the extension)

Postby Grigory » Tue Nov 26, 2002 1:46 pm

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: Select all
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
New php-forum User
New php-forum User
 
Posts: 6
Joined: Tue Nov 26, 2002 1:40 pm

Re: Verifying the type of image (ie. the extension)

Postby WiZARD » Wed Nov 27, 2002 12:23 am

Grigory wrote: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: Select all
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: Select all
switch ($type) {
  case "jpeg":
  case "jpg":
   $filename = "Images/$prodname.jpg";

User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol


Return to PHP coding => Images

Who is online

Users browsing this forum: Google [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.