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 7 of 7
  1. #1
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts

    $_FILES error problem

    I've been doing image work lately and normally I wouldn't like people re upload their images, due to these kinds of troubles.

    At any rate, I've been using the following code to try and stop the files from being replaced, as the re-upload is optional, if some one doesn't enter the field.
    PHP Code:
    if($_FILES['image_link']['error'] != UPLOAD_ERR_OK
        return; 
    Turns out that doesn't work.
    if the field isn't entered then the error would normally be 4. I can't figure out why it isn't working.

    Here is the rest of the actual image processing code.
    PHP Code:
    function process_image()
    {
    $upload_dir 'image/reviews/';
    $filename $_FILES['image_link']['tmp_name'];
    $new_side 75;


    if(
    $_FILES['image_link']['error'] != UPLOAD_ERR_OK
        return;

    if(!
    is_uploaded_file($filename))
        
    fatal_error('Sorry, you are obviously not trying to upload a picture.');

    if (!
    extension_loaded('gd')) {
        
    $prefix = (PHP_SHLIB_SUFFIX === 'dll') ? 'php_' '';
        
    dl($prefix 'gd.' PHP_SHLIB_SUFFIX);
    }

        list(
    $width$height$type) = getimagesize($filename);
        
    $imagetype image_type_to_mime_type($type);
        
        
        switch(
    $imagetype)
        {
        case 
    "image/jpeg":
        case 
    "image/jpg":
            
    $extension '.jpg';
            
    $newfile $upload_dir.$_SESSION['user']->user_id.'_'.time().$extension;
            
            
    $w $width;
            
    $h $height;
        
            if (
    $h $w) {
                
    $new_w = ($new_side $h) * $w;
                
    $new_h $new_side;    
            } else {
                
    $new_h = ($new_side $w) * $h;
                
    $new_w $new_side;
            }
        
            
    $im2 imagecreatetruecolor($new_w$new_h);
            
    $image imagecreatefromjpeg($filename);
            
    imagecopyresampled($im2$image0000$new_w$new_h$w$h);
            
    imagejpeg($im2$newfile75);
            
            return 
    $newfile;

        break;

        case 
    "image/gif":
              
    $extension '.gif';
            
    $newfile $upload_dir.$_SESSION['user']->user_id.'_'.time().$extension;
            
            
    $w $width;
            
    $h $height;
        
            if (
    $h $w) {
                
    $new_w = ($new_side $h) * $w;
                
    $new_h $new_side;    
            } else {
                
    $new_h = ($new_side $w) * $h;
                
    $new_w $new_side;
            }
        
            
    $im2 imagecreatetruecolor($new_w$new_h);
            
    $image imagecreatefromgif($filename);
            
    imagecopyresampled ($im2$image0000$new_w$new_h$w$h);
            
    imagegif($im2$newfile);
            
            return 
    $newfile;

          break;

        case 
    "image/png":
              
    $extension '.png';
            
    $newfile $upload_dir.$_SESSION['user']->user_id.'_'.time().$extension;
            
            
    $w $width;
            
    $h $height;
        
            if (
    $h $w) {
                
    $new_w = ($new_side $h) * $w;
                
    $new_h $new_side;    
            } else {
                
    $new_h = ($new_side $w) * $h;
                
    $new_w $new_side;
            }
        
            
    $im2 imagecreatetruecolor($new_w$new_h);
            
    $image imagecreatefrompng($filename);
            
    imagecopyresampled ($im2$image0000$new_w$new_h$w$h);
            
    imagepng($im2$newfile0PNG_NO_FILTER);
            
            return 
    $newfile;

         break;
        
        case 
    "image/bmp":
            
    fatal_error('Please, do NOT upload any .bmp (MS paint or equivalent) files. They can not be used in the server.');
        break;
        default:
            
    fatal_error('Please take note, you have uploaded an invalid file. The site only accepts the following images. '."\n".
            
    '.jpg/.jpeg, .gif, and .png type files. ','Attention');
        break;
        }        


    I get a feeling that maybe I should just stick to numbers, the constant may not be a constant with my host...
    Thanks for the help guys.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Digicoder View Post
    PHP Code:
    if($_FILES['image_link']['error'] != UPLOAD_ERR_OK
        return; 
    try this:

    PHP Code:
    if($_FILES['image_link']['error'] !== UPLOAD_ERR_OK
        return; 
    best regards

  • #3
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Nope didn't work.

    Any other suggestions?
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!

  • #4
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Digicoder View Post
    Nope didn't work.

    Any other suggestions?
    1. try somewhere outside the function a:
    PHP Code:
    print_r($_FILES); 
    to see the valus.

    2. check if 'if' work:

    PHP Code:
    if($_FILES['image_link']['error'] != UPLOAD_ERR_OK) {
        print 
    "This work";
        return;

    3. it's better to pass the value as argument to the function the using with file scope, so you can try

    if consider:

    PHP Code:
    $filename $_FILES['image_link']['tmp_name'];
    $okupload $_FILES['image_link']['error']; 
    become:

    PHP Code:
    function process_image($filename$okupload)
    {
    $upload_dir 'image/reviews/';
    $new_side 75

    if(
    $okupload != UPLOAD_ERR_OK)
       return; 
    or pass them as array.

    4. is paranoic but you could check,

    PHP Code:
    if(UPLOAD_ERR_OK == 4)
      print 
    "This work"
    I don't have anything else in mind, .

    best regards
    Last edited by oesxyl; 01-22-2008 at 02:05 AM.

  • #5
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Well, see that the problem I did check it that way.

    the error returned 4.
    I think that its the version of PHP really.
    Don't know when the constants where added but the version my host is using is 5.1.4 I believe.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by Digicoder View Post
    Well, see that the problem I did check it that way.

    the error returned 4.
    I think that its the version of PHP really.
    Don't know when the constants where added but the version my host is using is 5.1.4 I believe.
    PHP 4.2.0:

    http://www.php.net/manual/en/feature...oad.errors.php

    get info about php:

    upload on server a file with:

    PHP Code:
    <?php phpinfo(); ?>
    best regards

  • #7
    New Coder
    Join Date
    Sep 2007
    Location
    US
    Posts
    88
    Thanks
    4
    Thanked 4 Times in 4 Posts
    Gah, I feel stupid now.

    I remembered that not to long about the way I have used some of the operators in my scripts stopped working correctly.

    The problem I've been having all along was that
    PHP Code:
    if($_FILES['image_link']['error'] == UPLOAD_ERR_OK)
    {
    //... 
    is what works.

    Thanks for the help, I should probably take a nap or something and set my mind straight again.
    I need to find a book about all this stuff. God, thats gonna be one big book!

    http://www.gamezftw.com
    Play On!


  •  

    Posting Permissions

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