Image upload script works fine in IE but not in firefox

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Shib
Forum Newbie
Posts: 2
Joined: Mon Nov 23, 2009 3:04 pm

Image upload script works fine in IE but not in firefox

Post by Shib »

I was wondering if somebody could take a look at this. This script is supost to upload an image (restricting sizes and file types) and then the file is re-sized.

Anyways, the script works fine in IE but in firefox it can't seem to find the file type.

the error occurs somewhere around line 48, I am not to sure.

Code: Select all

 
<?php
echo '
<form action="test.php" method="post" enctype="multipart/form-data" id="something" class="uniForm">
        Description: <input name="desc" id="desc" type="text" /><br> 
        Image Location: <input name="new_image" id="new_image" size="30" type="file" class="fileUpload" /><br>
        <button name="submit" type="submit" class="submitButton">Upload</button>
</form>
';
    $stop = '0';
        if(isset($_POST['submit'])){
          if (isset ($_FILES['new_image'])){
              $imagename = time().$_FILES['new_image']['name'];
              $source = $_FILES['new_image']['tmp_name'];
              $file_size = $_FILES['new_image']['size'];
              $size_limit = '2000000';
              $target = "locks/".$imagename;
              $file_type = $_FILES['new_image']['type'];
              
                    if($file_size >= $size_limit) :
                            echo 'You image is to large!';
                    else :
                        if($_FILES['new_image']['type'] == 'image/pjpeg'):
                            move_uploaded_file($source, $target);
                        elseif($_FILES['new_image']['type'] == 'image/x-png'):
                            move_uploaded_file($source, $target);
                        elseif($_FILES['new_image']['type'] == 'image/gif'):
                            move_uploaded_file($source, $target);
                        endif;
                    endif;
              
              
                $imagepath = $imagename;
                $save = "locks/" . $imagepath; //This is the new file you saving
                $file = "locks/" . $imagepath; //This is the original file
              $x = getimagesize($file); 
        switch($x[2]) { 
            case 1: 
                    $image = imagecreatefromgif($file); 
             break; 
            case 2: 
                    $image = imagecreatefromjpeg($file);
                break; 
            case 3: 
                    $image = imagecreatefrompng($file);  
             break; 
            default: 
                    echo "file is not a valid image file.<br>"; 
                    $stop = '1';
                break;
        } 
           if($stop != 1) {
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = $width;
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
              /*
              $file_type = $_FILES['new_image']['type'];
        if($file_type == "image/jpeg" || $file_type == "image/jpg") :
            $image = imagecreatefromjpeg($file);
        elseif($file_type == "image/x-png" || $file_type == "image/png") :
            $image = imagecreatefrompng($file);
        elseif($file_type == "image/gif") :
            $image = imagecreatefromgif($file);
            else :  
                echo 'Invalid type';
        endif;*/
         
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 100) ; 
 
              $save = "locks/thumbs/sml_" . $imagepath; //This is the new file you saving
              $file = "locks/" . $imagepath; //This is the original file
 
              list($width, $height) = getimagesize($file) ; 
 
              $modwidth = 200; 
 
              $diff = $width / $modwidth;
 
              $modheight = $height / $diff; 
              $tn = imagecreatetruecolor($modwidth, $modheight) ; 
          $x = getimagesize($file); 
        switch($x[2]) { 
            case 1: 
                    $image = imagecreatefromgif($file); 
             break; 
            case 2: 
                    $image = imagecreatefromjpeg($file);
                break; 
            case 3: 
                    $image = imagecreatefrompng($file);  
             break; 
            default: 
                    echo "file is not a valid image file."; 
            } 
              imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ; 
 
              imagejpeg($tn, $save, 100) ; 
              /*
                $id = mysql_real_escape_string($_SESSION['id']);
                $clean_model = mysql_real_escape_string($model);
                $desc = mysql_real_escape_string($_POST['desc']);
                $lock_id = mysql_real_escape_string($lock_id);
                $saved_location = mysql_real_escape_string($imagename);
                $time = time();
                mysql_query("INSERT INTO `locks_pics` (`id` ,`lock_id` ,`saved` ,`time` ,`desc` ,`owner` )VALUES (NULL , '$lock_id', '$saved_location', '$time', '$desc', '$id')");
        header("Location: ../lockdb.php?func=5&id=$lock_id");
        */
        echo 'No problem :)';
          }
     }
        }
?>
 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Image upload script works fine in IE but not in firefox

Post by requinix »

You're basing your script off of what IE tells you - except what it tells you is wrong. It's one more thing that IE screws up.

$_FILES[...]["type"] cannot be trusted. Ever. For images, use getimagesize to determine the type.

Code: Select all

$info = getimagesize($_FILES["new_image"]["tmp_name"]);
if ($info === false) {
    // not an image, or not an image that getimagesize recognizes
} else if ($info[2] == IMAGETYPE_JPEG) {
    // jpeg image
} else if ($info[2] == IMAGETYPE_GIF) {
    // gif image
} else if ($info[2] == IMAGETYPE_PNG) {
    // png image
} else {
    // some other type of image: bmp, psd, tiff...
}
Shib
Forum Newbie
Posts: 2
Joined: Mon Nov 23, 2009 3:04 pm

Re: Image upload script works fine in IE but not in firefox

Post by Shib »

Thank you :)

The script works fine now!
Post Reply