警告:getimagesize(Array)[function.getimagesize]:无法打开流:

时间:2012-01-19 19:41:20

标签: php getimagesize

我的图片上传代码中出现以下错误:

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

以下是目前的代码:

$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>';
}

}

以下是print_r

$_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 个答案:

答案 0 :(得分:1)

看起来你正在使用多文件PHP数组名称设置:

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

这意味着

$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

不正确。你应该使用

 $temp_dir = $_FILES['files'][$key]['tmp_name'];
                            ^^^^^^--- missing this part.
 $image_type = etc....

同样,请勿使用isset($_FILES)。这是不可靠的。您应该使用每个文件的各个子阵列上的['error']属性检查上传失败/成功:

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...
}

答案 1 :(得分:0)

这是

// 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()无法找到该文件。使用var_dump()

检查您的给定路径(getimagesize()的参数)