Warning: getimagesize(Array) [function.getimagesize]: failed to open stream:

5.8k views Asked by At

I get the following error in my image upload 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 so far:

$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>Cannot upload image, file error: '.  $_FILES['files'][$key].'</p>';
        }
    } // End of foreach

}else{
    echo '<p>Cannot upload image, no image selected for uploading.</p>';
}

}

Here is a print_r of $_FILES

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

There are 2 answers

1
Marc B On BEST ANSWER

it appears you're using the multi-file PHP array name setup:

<input type="file" name="file[]">
<input type="file" name="file[]">

which means that

$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

is incorrect. You should be using

 $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:

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...
}
0
moe On

It's

// Get image info
$temp_dir = $_FILES['files'][$key]['tmp_name']; // Temporary location of file
$image_type = $_FILES['files'][$key]['type']; // Image filetype
$image_size = $_FILES['files'][$key]['size']; // Image file size
$image_name = $_FILES['files'][$key]['name']; // Image file name

PS: getimagesize() can't find the file. Check your given path (parameter of getimagesize()) with var_dump()