Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Upload script fails on IE

    I'm not to sure if server side code would be effected or by different browsers but this script doesn't work on internet explorer. It fails every time which I can't see why it is failing for. If I could some help on this then that would be great.

    PHP Code:

    <?php
    session_start
    ();
        class 
    imageHandler {
        
        public 
    $submit// The submit button name
        
    public $location// The upload location to be uploaded at
        
    public $name// The name of the file
        
    public $tmp_name;
        public 
    $type// The file type
        
    public $size// The file size
        
        
    public function conn($link) {
        if(
    $link) {
            require(
    $link);
        }
        }
        
        public function 
    validate() { //make sure that this file is a image
            
    $valid = array(=> 'png'=> 'jpg'=> 'jpeg'=> 'gif');
            
            if(!
    in_array($this->type$valid)) {
            return 
    false;
            }
            
        return 
    true;
        }
            
        public function 
    newname($rename) {
        
        if(
    $this->validate() == true && isset($_POST[$this->submit])) {
        
        
    //first thing is to remove the file extension
            
    $remove = array(=> '.png'=> '.jpeg'=> '.jpg'=> '.gif');
            
            
    $noExt str_replace($remove''$this->name);
            
    //we will also check to make sure that there isn't any white spaces
            
    $noExt str_replace(' ''_'$noExt);
        
        
    //now we are going to check if we are wanting to rename    
        //this photo with a rand digit or use the previous name
        //now so we can refer it again in the database in a different
        //directory
        
        
    if($rename == true || !isset($_SESSION['newname']) || $_SESSION['noExt'] !== $noExt) {
        
            if(isset(
    $_SESSION['newname'])) {
            unset(
    $_SESSION['newname']);
            }
            
        while(
    file_exists($this->location.$newname)) {
            
    $rand rand(0999999);
            
    $newname $noExt.'-'.$rand.'.'.$this->type;
            
    $_SESSION['newname'] = $newname;
            
    $_SESSION['noExt'] = $noExt;
        }
        
        }
        else
        {
        
    $newname $_SESSION['newname'];
        }
        
        return 
    $newname;
        
        }
        else 
        {
        return 
    false;
        }
        
        }
        
        public function 
    upload($new_width$new_height$db) {
        if(!
    file_exists($this->location.$this->newname(false))) { //Dont allow any file to be uploaded that already exists
        
            
    if($new_width == false && $new_height == false) { // If there is going to be no image resize}
            
    copy($this->tmp_name$this->location.$this->newname(false));
            }
            
            if(
    $db == true) {
            
    //Also remove the users old image to save space
            
    $rSql "SELECT photo FROM users WHERE username = '".$_SESSION['username']."'";
            
    $rQuery mysql_query($rSql);
            
    $last mysql_fetch_assoc($rQuery);
            
    $_SESSION['last'] = $last['photo'];
            
            if(
    file_exists($this->location.$_SESSION['last']) && !empty($_SESSION['last'])) { //run a check to make sure the file is there
            
    unlink($this->location.$_SESSION['last']);
            }
            
            }
            else {
            if(
    file_exists($this->location.$_SESSION['last']) && !empty($_SESSION['last'])) { //run a check to make sure the file is there
            
    unlink($this->location.$_SESSION['last']);
            }
            }
            
            
            if(
    $db == true) { // Save this image as the user profile image
                
    $sql "UPDATE users SET photo = '".$this->newname(false)."' WHERE username = '".$_SESSION['username']."'";
                
    $query = @mysql_query($sql);
                
                if(!
    $query) {
                die(
    'Server Error: ' mysql_error());
                }
            }
            
            if(
    $new_width == true || $new_height == true) { // Image is set to be resized.
            
            
    list($width$height) = getimagesize($this->tmp_name);
            
    $image_p imagecreatetruecolor($new_width$new_height);

            switch(
    $this->type) { //switch between different file types
                
    case 'jpeg':
                
    $image imagecreatefromjpeg($this->tmp_name);
                break;
                case 
    'jpg':
                
    $image imagecreatefromjpeg($this->tmp_name);
                break;
                case 
    'gif':
                
    $image imagecreatefromgif($this->tmp_name);
                break;
                case 
    'png':
                
    $image imagecreatefrompng($this->tmp_name);
                break;
            }

            
    imagecopyresampled($image_p$image0000$new_width$new_height$width$height);

            switch(
    $this->type) {
                case 
    'jpeg':
                
    imagejpeg($image_p$this->location.$this->newname(false), 100);
                break;
                case 
    'jpg':
                
    imagejpeg($image_p$this->location.$this->newname(false), 100);
                break;
                case 
    'gif':
                
    imagegif($image_p$this->location.$this->newname(false), 100);
                break;
                case 
    'png':
                
    imagepng($image_p$this->location.$this->newname(false), null);
                break;
            }
            }
        
        }
      }
    }
    ?>
    And then I called it like this...

    PHP Code:
    <?php
    if(isset($_POST['submit'])) {
    include(
    '../scripts/class.php');
        
        
    $image = new imageHandler();
        
    $image->submit 'submit';
        
    $image->tmp_name $_FILES['photo']['tmp_name'];
        
    $image->name $_FILES['photo']['name'];
        
    $image->type basename($_FILES['photo']['type']);
        
    $image->conn('../conn.php');
        
        
    $image->location '../../uploads/profile/';
        
    $image->newname(false);
        
    $image->upload(220,166,true);
        
    }
        
    ?>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <body>
    <form action="upload.php" method="post" enctype="multipart/form-data">
    <input name="photo" type="file" />
    <input name="submit" type="submit" value="Upload" />
    </form>
    </body>
    </html>
    Thanks for the help.

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    nothing particular stands out to me while looking at your code.

    though a few things seem a bit strange, for example, I don't see where your getting the $type from. I see that you pass it the basename, then in the class try to compare it to file extentions?

    also this part seems a bit odd to me:
    PHP Code:
        while(file_exists($this->location.$newname)) {
            
    $rand rand(0999999);
            
    $newname $noExt.'-'.$rand.'.'.$this->type;
            
    $_SESSION['newname'] = $newname;
            
    $_SESSION['noExt'] = $noExt;
        } 
    why would you use session for this area, when the rest of the script is passing data back to the class?


    have you tried debugging?
    ie: go down the script one step at a time and see where it's stopping.

  • #3
    bdl
    bdl is offline
    Regular Coder
    Join Date
    Apr 2007
    Location
    Camarillo, CA US
    Posts
    590
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml">
    Why are you using an XML namespace with HTML4? Did you mean to use an XHTML DTD? This could be why IE is having a hard time.

    Otherwise, please explain what is / how it is failing.

  • #4
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I doubt its really the script is the problem but I'm open to options too. I've took a look to what you guys said I haven't tried debugging yet because the script runs fine on other browsers.

    How its failing is you select a image to upload and you click upload it acts like it is uploading but then it seems like it times out and stops uploading. When I go and check the photo has not been updated. I'm really confused by this whole thing because I know IE has problems but I thought the server side script run in the background on the server and not really in the browser like JavaScript and other markup languages. I'll give debugging a try and see what I come up with.

  • #5
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    What versions of IE does it fail in or what versions of IE have you tried?
    ||||If you are getting paid to do a job, don't ask for help on it!||||

  • #6
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Internet explorer 8.

  • #7
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Does anyone have any idea what the problem might be with this? I still haven't got this one resolved.

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by Jon W View Post
    Does anyone have any idea what the problem might be with this? I still haven't got this one resolved.
    Quote Originally Posted by Jon W View Post
    I haven't tried debugging yet because the script runs fine on other browsers.

    Neither will you unless you follow the correct steps for isolating a problem. Step number one usually being actually trying to isolate the problem, funnily enough.

  • #9
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Okay so for some reason IE is reading jpeg files a pjpeg.... Apparently it stand for protected jpeg... I wonder who was the genius at microsoft decided to rename the mime type for jpegs... So what was happening was the script was failing every time it got the point where it would check to see if it was a valid file type... so I added a line in the code to allow this file types... But now I get a new error saying:

    Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/tech/scripts/class.php on line 131

    Hrmm...
    Last edited by Jon W; 02-03-2010 at 02:47 PM.

  • #10
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    Okay, I got this problem resolved. Thanks guys.

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    what was the issue?

  • #12
    Regular Coder
    Join Date
    Jan 2008
    Posts
    334
    Thanks
    9
    Thanked 0 Times in 0 Posts
    I had a few switch that wasn't allowing the pjpeg. So I just added the pjpeg and it is now correctly working. After fixing this problem shortly after thinking I didn't have any more problem I tried uploading a png. png are being read as x-png in IE, so again I had to allow it to be used. And that was pretty much it.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •