+ Reply to Thread
Results 1 to 2 of 2

Thread: Image Upload Method error

  1. #1
    Junior Member
    Join Date
    Jan 2012
    Posts
    31

    Image Upload Method error

    Hi I get the following error when i upload an image :
    Code:
    Warning: getimagesize(Array) [function.getimagesize]: failed to open stream: No such file or directory in H:\Programs\webserver\root\media\images\upload.php on line 41
    Here is the code i wrote :
    Code:
    $submit = $_POST['submit'];
    if(isset($submit)){ // check if user has submitted a the form
    if(isset($_FILES['files'])){ // check if a file has been submitted in the form
        foreach($_FILES['files']['tmp_name'] as $key => $tmp_name){ // Loop through each Image if more than one image has been uploaded
    
            // Get image info
            $temp_dir = $_FILES['files']['tmp_name']; // Temporary location of file
            $image_type = $_FILES['files']['type']; // Image filetype
            $image_size = $_FILES['files']['size']; // Image file size
            $image_name = $_FILES['files']['name']; // Image file name
    
            // Get image width and height
            $image_dimensions = getimagesize($temp_dir); // returns an array of image info [0] = width, [1] = height
            $image_width = $image_dimensions[0]; // Image width
            $image_height = $image_dimensions[1]; // Image height
    
            // Get form info
            $category = $_POST['cat'];
            $create_album = $_POST['album'];
            $select_album = $_POST['choose_album'];
    
            if($_FILES['files']['error'][$key] = UPLOAD_ERR_OK){ // Check to make sure there are no errors in the file ( 0 )
                //$filetype = ($type == 'image/jpeg') ? '.jpeg': str_replace('image/','',$type);
    
                // Make sure each filename name is unique when it is uploaded
                $random_name = rand(1000,9999).rand(1000,9999).rand(1000,9999).rand(1000,9999);
    
                // Set the path to where the full size image will be stored
                $path = 'img/'.$random_name . $_FILES['files']['name'][$key];
    
                // Set the path to where the thumb image will be stored
                $thumb_path = 'img/thumb_/'.$random_name .$_FILES['files']['name'][$key];
    
                // Set the Maximum dimensions the images are allowed to be
                $max_width = 4040;
                $max_height = 4040;
    
                // Set the Maximum file size allowed (5MB)
                $max_size = 5242880;
    
                // Set the file extensions that are allowed to be uploaded and store them in an array
                $allowed = array('image/jpeg','image/png','image/gif');
    
                if(in_array($image_type,$allowed)){ // Check to make sure the image that is being uploaded has a file extension that we permit
                    if(($img_width < $max_width) && ($img_height < $max_height)){ // Check to make sure the Image dimensions do not exceed the maximum dimensions allowed
                        if($image_size < $max_size){ // Check to make sure the Image filesize does not exceed the maximum filesize allowed
                            $input_album = (!empty($create_album)) ? $create_album: $select_album;// Check to make sure the user has created an album or has select an existing album
                            // Check the shape of the Image (square, standing rectangle, lying rectangle) and assign a value depening on which it is
                            $case = image_shape($image_width, $image_height);
                            echo $case;
                        }else{
                            echo '<p>Error uploading '. $image_name .': Image filesize exceeds permitted filesize ( '. $max_size .' )</p>';
                        }
                    }else{
                        echo '<p>Error uploading '. $image_name .': Image dimensions exceed permitted dimensions ( '.$max_width.' x '. $max_height .' )</p>';
                    }
    
                }
    
            }else{
            echo '<p>Failed to upload image, file error: '.  $_FILES['files'][$key].'</p>';
            }
        } // End of foreach
    
    }else{
        echo '<p>Failed to upload image.</p>';
    }
    
    }
    Here is a print_r of the $_FILES
    Code:
    Array ( [files] => Array ( [name] => Array ( [0] => fyros_09.jpg ) [type] => Array ( [0] => image/jpeg ) [tmp_name] => Array ( [0] => C:\Users\chris-vaio\AppData\Local\Temp\php7754.tmp ) [error] => Array ( [0] => 0 ) [size] => Array ( [0] => 122773 ) ) )

  2. #2
    It appears you're using the multi-file PHP array :
    Code:
    <input type="file" name="file[]">
    <input type="file" name="file[]">
    which means that
    Code:
     // Temporary location of file
    $temp_dir = $_FILES['files']['tmp_name'];
     // Image filetype
    $image_type = $_FILES['files']['type'];
     // Image file size
    $image_size = $_FILES['files']['size'];
    // Image file name
    $image_name = $_FILES['files']['name'];
    is incorrect. You should be using
    Code:
     $temp_dir = $_FILES['files'][$key]['tmp_name'];
                                ^^^^^^--- missing this part.
     $image_type = etc....
    as well, don't use isset($_FILES). That's unreliable. You should be checking for upload failure/success using the ['error'] attribute on each file's individual sub-array:
    Code:
    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name) {
       if ($_FILES['files'][$key]['error'] !== UPLOAD_ERR_OK) {
            die("File #$key failed with error code " . $_FILES['files'][$key]['error']);
       }
       etc...
    }

+ Reply to Thread

Tags for this Thread

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