Image upload script.

  • genxservers
  • Novice
  • Novice
  • genxservers
  • Posts: 30

Post 3+ Months Ago

I am writing an image upload script for my website, and ran into a wierd little thing.

Seems like all my test image, be they .jpg, .jpeg will still register as image/pjpeg.

Now on the php.net documentation, they only mentioned image/jpeg and not image/pjeg.

If I allow the upload of only pjpeg type, would I be banning the upload of some legit jpeg pictures?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • _Leo_
  • Proficient
  • Proficient
  • User avatar
  • Posts: 279
  • Loc: Buenos Aires, Argentina

Post 3+ Months Ago

How are you checking the file type?
  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Posts: 480
  • Loc: India

Post 3+ Months Ago

i will give u the code to do it

$upload_file=$userfile_name[0];
$upload_size=$userfile_size[0]/1024;
$upload_type=$userfile_type[0];
$upload_tmp=$HTTP_POST_FILES['userfile']['tmp_name'][0];

if(substr($upload_type,0,5)!="image")
{
echo "Sorry the file is not a valid picturess";
exit;
}

if(strtoupper((substr($upload_type,strlen($upload_type)-4,strlen($upload_type))))=="JPEG")
{
$ext="JPEG";
}
elseif(strtoupper((substr($upload_type,strlen($upload_type)-3,strlen($upload_type))))=="GIF")
{
$ext="GIF";
}


as i have checked the image type u can also check it and set the appropriate image type
  • Carnix
  • Guru
  • Guru
  • User avatar
  • Posts: 1098

Post 3+ Months Ago

I've seen this as well. since nowhere in your script (that you pasted in anyway) are you specifically testing for $type == "image/pjpeg" (for example) then you're fine. If there is some place where you're testing for that, then use ($type == "image/pjpeg" || $type = "image/jpeg")
.c

*EDIT
Forgot to add:
With the ASP BinaryWrite method, you do have to specifically test the MIME return, and I use (image_type = "image/pjpeg" OR image_type = "image/jpeg") just to be safe, but I haven't seen image/jpeg as a MIME type ever since I wrote my first server-side upload script a few years ago. PHP makes it easier since it has a lot of these sorts of feature built right in. I think assuming pjpeg will be fine.
  • Rat
  • Guru
  • Guru
  • User avatar
  • Posts: 1190
  • Loc: desk

Post 3+ Months Ago

why type of upload image script is it?
  • gsv2com
  • Professor
  • Professor
  • User avatar
  • Posts: 776
  • Loc: Nippon

Post 3+ Months Ago

Try this. Yours (tannu4u) seems a little long winded. A lot of the code seems unneccessary.

Code: [ Select ]
if(!$_FILES['file']['name']){
    $error[]='You must submit a picture for this product';
} else {
    if($_FILES['file']['size']==0){
        $error[]=ucfirst($_FILES['file']['name']) . ' seems to be corrupt. It is 0kb in size (no data)';
    } elseif($_FILES['file']['size']>102400){
        $error[]='The file you submitted is too large! 100kb max! ' . ucfirst($_FILES['file']['name']) . ' is ' . ceil($_FILES['file']['size']/1024) . 'kb!';
    }
        
    if(($_FILES['file']['type']!='image/pjpeg') and ($_FILES['file']['type']!='image/gif')){
        $error[]='You must submit either a JPEG or GIF Image. ' . ucfirst($_FILES['file']['name']) . ' is a ' . $_FILES['file']['type'] . ' file!';
    }
}
  1. if(!$_FILES['file']['name']){
  2.     $error[]='You must submit a picture for this product';
  3. } else {
  4.     if($_FILES['file']['size']==0){
  5.         $error[]=ucfirst($_FILES['file']['name']) . ' seems to be corrupt. It is 0kb in size (no data)';
  6.     } elseif($_FILES['file']['size']>102400){
  7.         $error[]='The file you submitted is too large! 100kb max! ' . ucfirst($_FILES['file']['name']) . ' is ' . ceil($_FILES['file']['size']/1024) . 'kb!';
  8.     }
  9.         
  10.     if(($_FILES['file']['type']!='image/pjpeg') and ($_FILES['file']['type']!='image/gif')){
  11.         $error[]='You must submit either a JPEG or GIF Image. ' . ucfirst($_FILES['file']['name']) . ' is a ' . $_FILES['file']['type'] . ' file!';
  12.     }
  13. }


Then, just make a loop to output the errors onto the screen. A little more graceful than simple echo statements. More flexible.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 142 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.