file upload issue

They have: 461 posts

Joined: Jul 2003

currently my procedssing uploaded files looks like this:(note, returns added to prevent horizontal scroll bar)

<?php

   
else{ # only bother to process any uploaded
#files and set the account to being usable if the confcode is right
     
$images=array('main'=>'Main picture', 't1'=>'1st Thumbnail', 't2'=>'2nd Thumbnail',
't3'=>'3rd Thumbnail', 't4'=>'4th Thumbnail', 'salute'=>'Salute');
      foreach(
$images as $key=>$value){
   if(
$_FILES[$key]['name']){ # if they uploaded a file
    
if($_FILES[$key]['error'] !== (0 || 'UPLOAD_ERR_OK')){ # if there was an error
      
$picerr=TRUE; $warn=TRUE; $error=$_FILES[$key]['error'];
      
$warns[]=\"Uploading your $value caused an error: $error\";
     }
     if(153600<
$_FILES[$key]['size']){ # make sure it isn't over 150 KB
      
$picerr=TRUE; $warn=TRUE;
      
$warns[]=\"Your $value was too large. You may not upload a
#file over 153600 Bytes (150 KB)\";
     }
     if('image/jpeg'!==
$_FILES[$key]['type']){ # only accept jpegs
      
$picerr=TRUE; $warn=TRUE;
      
$warns[]=\"Your $value was not a JPEG. JPEG encoded files traditionally end
with .jpe, .jpg, and .jpeg on windows.\";
     }
     if(!(
$picerr)){ # if there wasn't an issue, move to the awaiting approval bin --
# humans will check it's ok
      
$un=$_COOKIE['un']; $to='/var/www/html/findyourdesire/unapproved/'.$key.'.'.
$un.'.jpg';
       move_uploaded_file(
$_FILES[$key]['tmp'], $to);
      
$warns[]=\"$value was uploaded sucessfully\"; # incase something else went wrong
     }
   }
      }

?>
however, that ALWAYS returns the error on the type. so i know it doesn't return it as 'image/jpeg' like i expected. does anyone know how it does denote them? i would prefer to know how it denotes thenm than to have someooone fix the code, because by being told how it denotes them i can avoid this problem in the future

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

They have: 93 posts

Joined: Jun 2003

I skipped the method and used this, it worked more exact for me, I had similar issues

$imagesize = getimagesize($source);
switch ($imagesize[2] ){
case 0:
$errorm .= "Invalid File Type for Screen";
$eflag = true;
break;
case 1:
$file = $imgname . '.gif';
$eflag = false;
break;
case 2:
$file = $imgname . '.jpg';
$eflag = false;
break;
case 3:
$errorm .= "Invalid File Type for Screen";
$eflag = true;
break;
}

the $source is from $_FILES['fieldname']['tmp_name'].

If you dont want to use that method, check your encryption type and make sure the form tag has this ENCTYPE="multipart/form-data

[Design Alpha] -Web Services : Design,Hosting,Advertising,Software
Ask about custom pricing on hosting!!
Site Assets: [UltraGaming.com] [Blades of Warcraft]

They have: 461 posts

Joined: Jul 2003

i would prefer to know how it denotes them than to have someone fix the code, because by being told how it denotes them i can avoid this problem in the future

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Oooooh, calm down! Laughing out loud
The answer is there, you just need to have the patience to see it.

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

When in doubt, print it out.

<?php
if('image/jpeg'!==$_FILES[$key]['type']){ # only accept jpegs
   
$picerr=TRUE; $warn=TRUE;
   
$warns[]=\"Your $value was not a JPEG. JPEG encoded filestraditionally end
    with .jpe, .jpg, and .jpeg on windows.\";
    echo \"<br />\n<b>Image Type: \".
$_FILES[$key]['type'].\"<br />\n\";
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

eregi('^image/p?jpeg(;.*)?$', $_FILES['uploadedfile']['type'])

eregi('^image/gif(;.*)?$', $_FILES['uploadedfile']['type'])

They have: 461 posts

Joined: Jul 2003

the problem with checking the extension instead is something i discovered elsewhere.... namely in dealing with less computer competent friends....

turns out there's a good deal of people that think changing the extension to .jpg will magically encode a file to jpeg format.

and searching 'photoname.extension' is bad because it's valid to have 'photoname.date.extension' and i for one do that....but like i said, both methods fail due to the stupidity of the genereal human populace.

at p2p.wrox someoen pointed out thatdepending ont he browser/os it can be image/jpg image/jpeg image/pjpeg
i didn't realize that. and the person said that's all he knows and also pointed out the browser wont always guess even if i have the encryption/multipart form data tag in my form call.

this begs the question: anyone know how to parse the begining of an image file? it'll def be a lot, but it'll also be a way around the human stupidity factor.

for those with less diverse os experience, windows is the ONLY os that relies on extension to tell file type. ALL other operating systems i have ever used do that by the header of the file. thus if i knew how to parse the begining of pictures files in ascii, i could check that (using the tmp name) and get the type from there. and this is something i would def send to php.net because i'm SURE that anyone else making a site where people upload pics that they want to restrict to a certain type would want to use this function.

anyone wanna help me with that?

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

look up a post. Wink

those will get you the image types, not the extensions.

They have: 461 posts

Joined: Jul 2003

exactly. i want to know TYPE, not extension. the first fix given to me does extension, not type. and since php doesn't have gif in the gd it uses, if i accept anything other than jpegs, it'll be pngs. right now i just want jpgs. your fix works WHEN the browser gusses, but only in two of the three instances i know of. i'm looking for one that will be universal, including the browser not guessing and leaving it blank (i know a few people that prevent their browsers from doing that)

edit:
btw: i just found this on one of the other sites i'm also part of:
there's something called iptcparse() which parses the iptc file header (what non windows systems use to tell things about images, since images use the iptc header). that function was too complex for my liking, but said to look at getimagesize.... turns out the 3rd of the 4 things it returns is actually the image TYPE (i think according to the iptc header) which means the only people that will fool it have to be good enough with images that i'd never be able to compete at my crrent level, so i'm not going to bother trying. (unless someone that high decides to make an image-f*cking-with kit like that worm/trojan/virus kit that got into the news a few years back...but why someone would WANT to help script kiddies is beyond me)

POSIX. because a stable os that doesn't have memory leaks and isn't buggy is always good.

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

ooooooohhh, I get it know. Interesting problem. Sorry I wasn't able to help. Glad PHP was, though. Smiling

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.