SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    getimagesize in multiple upload!?!?

    I think I have tryied everything, but can't seem to find the error... Please help.

    I keep getting this error:
    Notice: getimagesize() [function.getimagesize]: Read error! in function.gallery.php on line 475

    Here is the part of the script:
    PHP Code:
        foreach($_FILES as $k => $v){ 

                if( !
    $_FILES[$k]['error'] && preg_match("#^image/#i"$_FILES[$k]['type']) && $_FILES[$k]['size'] < 100000000){
                    
                    
    $imagename $_FILES[$k]['name'];
                      
    $tempFile $_FILES[$k]['tmp_name'];
                      
    $targetPath $folder;
                      
    $targetFile =  str_replace('//','/',$targetPath) . $_FILES[$k]['name'];
                    
                    
    move_uploaded_file($tempFile,$targetFile);

                    
    $imagepath $imagename;
                    
                      
    $save $targetPath."".$imagepath//This is the new file saving
                      
    $file $targetPath."".$imagepath//This is the original file
     
                      
    list($width$height) = getimagesize($file); // THIS IS WHERE IT GOES WRONG?!?!? 
    Thanks in advance

  2. #2
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Debug your code line by line.
    Make sure that move_uploaded_file actually worked.
    Do something like if(false === move_uploaded_file($tempFile,$targetFile)){
    throw new Exception('could not move file: '.$tempFile.' to ' .$targetFile);
    }

    Then do similar things with $file
    something like this:
    if(!file_exists($file) || !is_readable($file)){
    throw new Exception('File '.$file.' does not exist or not readable');
    }

    and then with getimagesize:

    Basically you need to check your code line by line to find where the the problem is. Don't just assume that the problem is, as you said "THIS IS WHERE IT GOES WRONG?!?!? "

    Don't assume anything when it comes to debugging.

  3. #3
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, I may be wrong here, but I would handle the foreach differently

    You are doing foreach($_FILES as $k => $v){ }

    but you not even using the $v anywhere. The $v will represent one individual file, so you should use the $v instead of $_FILES[$k]

    If may look like these are the same, but then are not.

    Also you have this:
    !$_FILES[$k]['error']

    I would be more specific and put this check like this:
    if(0 === $v['error'])

    or maybe like this:
    if(empty($v['error'])

    Remember that the array element 'error' always present in each file upload, it's just that
    the value of 'error' will be 0 if there are no errors or > 0 if there are errors.

    You can add an echo to make sure your conditions are met:

    if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type']) && $_FILES[$k]['size'] < 100000000){
    echo 'got good image here';

    // rest of your code

    }

    Then see if you get at least one 'got good image here' in your browser output

  4. #4
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Allrighty... Tryied all the suggestions and tracked the first error:

    PHP Code:
    if( !$v['error'] && preg_match("#^image/#i"$v['type']) && $v['size'] < 100000000){
                    
                    echo 
    'Image is OK...';
                    
                    
    $imagename $v['name'];
                      
    $tempFile $v['tmp_name'];
                      
    $targetPath $folder;
                      
    $targetFile =  str_replace('//','/',$targetPath) . $v['name'];
                    
                    
    move_uploaded_file($tempFile,$targetFile);
                    
                    if(
    false === move_uploaded_file($tempFile,$targetFile)){
                        echo 
    'could not move file: '.$tempFile.' to ' .$targetFile;
                    } 
    It comes out with this:
    could not move file: /tmp/php66yRzL to images/myimage1.JPG

    Any suggestions?

  5. #5
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It comes out with this:
    could not move file: /tmp/php66yRzL to images/myimage1.JPG

    Any suggestions?[/QUOTE]

    Sure, use the full system path for the new destination.

    you are getting this error because images/ directory does not exist.

    If you know the full path to images dir, then just prepend the full path to it, like this (example)

    /usr/local/images/

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you have move_uploaded_file() twice now?

    move_uploaded_file($tempFile,$targetFile);



    if(false === move_uploaded_file($tempFile,$targetFile)){

    echo 'could not move file: '.$tempFile.' to ' .$targetFile;

    }

    You should remove the first like, leaving only the second where you have a test for 'false'

  7. #7
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't think thats it, allthough I just tryied what you said, but still the same error. The thing is that the image I upload does actully get uploaded, but when I try to get the "list($width, $height) = getimagesize($tempFile);" it goes wrong... I think it has something to do with the "tempFile" but not sure what?!?!?

  8. #8
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am pretty sure it's because you need a full path to the new destination, but if you thinkg it's something else, then I can't do anything else for you.
    Ok, try this,
    after the
    $targetPath = $folder;
    add this:
    if(!file_exists($targetPath)){
    echo 'no such dir: '.$targetPath;
    }

    If you get 'no such dir', then you know for sure that your problem is an incorrect path

  9. #9
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sharedlog.com View Post
    You should remove the first like, leaving only the second where you have a test for 'false'
    Sorry... missed that, moved it and are now back to this error:
    Image is OK...
    Notice: getimagesize() [function.getimagesize]: Read error! in /home/www/mypage.com/functions/function.gallery.php on line 483
    File: images/newimage1.JPG
    Image Width: - Height:
    Warning: Division by zero in /home/www/mypage.com/functions/function.gallery.php on line 495


    This is my script now:
    PHP Code:
    if( !$v['error'] && preg_match("#^image/#i"$v['type']) && $v['size'] < 100000000){
                    
                    echo 
    'Image is OK...';
                    
                    
    $imagename $v['name'];
                      
    $tempFile $v['tmp_name'];
                      
    $targetPath $folder;
                      
    $targetFile =  str_replace('//','/',$targetPath) . $v['name'];
                    
                    if(
    false === move_uploaded_file($tempFile,$targetFile)){
                        echo 
    'could not move file: '.$tempFile.' to ' .$targetFile;
                    }

                    
    $imagepath $imagename;
                    
                      
    $save $targetPath."".$imagepath//This is the new file saving
                      
    $file $targetPath."".$imagepath//This is the original file
                    
                    
    if(!file_exists($file) || !is_readable($file)){
                        echo 
    'File '.$file.' does not exist or not readable';
                    }
     
                      list(
    $width$height) = getimagesize($file); 
                    
                    echo 
    'File: '.$file.'<br>';
                    echo 
    'Image Width: '.$width.' - Height: '.$height.''
    Can't see the tree for the forest?!?!

  10. #10
    SitePoint Guru
    Join Date
    Dec 2005
    Posts
    964
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sharedlog.com View Post
    Ok, try this,
    after the
    $targetPath = $folder;
    add this:
    if(!file_exists($targetPath)){
    echo 'no such dir: '.$targetPath;
    }
    Inserted it but didn't get the error...?


Bookmarks

Posting Permissions

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