无法在php中获取上传文件的尺寸

时间:2014-02-14 08:25:18

标签: php file-upload getimagesize

需要获取上传文件的尺寸,例如宽度,高度,尺寸等。我尝试使用getimagesize(),但这不起作用。我收到错误,

Warning: getimagesize(C:/wamp/www/KSHRC/uploads/): failed to open stream: No such file or directory in C:\wamp\www\KSHRC\registration\multi_fileupload.php on line 31  

Notice: Array to string conversion in C:\wamp\www\KSHRC\registration\multi_fileupload.php on line 31  

这是代码,

for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
{
    $root = $_SERVER['DOCUMENT_ROOT']."/KSHRC/uploads/";    
    $filename = $_FILES['userfile']['name'][$i];
    echo $size = getimagesize($root.$filename);
}

请帮帮我..

1 个答案:

答案 0 :(得分:0)

您必须首先将文件从tmp移动到所需的文件夹,然后使用您的函数getimagesize

获取图像大小
for($i=0; $i < count($_FILES['userfile']['tmp_name']);$i++)
{
    $root = $_SERVER['DOCUMENT_ROOT']."/KSHRC/uploads/";    
    $filename = $_FILES['userfile']['name'][$i];

    move_uploaded_file($_FILES['userfile']['tmp_name'], $root.$filename);
    echo $size = getimagesize($root.$filename);
}

<强>更新

你也可以通过$_FILES['userfile']['size']获取文件大小

$size = $_FILES['userfile']['size'];

这将返回以字节为单位的大小

更新2

$ARR_FILES = $_FILES['userfile'];

 for($i=0; $i < count($ARR_FILES);$i++)
    {
        $root = $_SERVER['DOCUMENT_ROOT']."/KSHRC/uploads/";    
        $filename = $ARR_FILES[$i]['name'];
        $tmp_name = $ARR_FILES[$i]['tmp_name'];
        list($width, $height, $type, $attr) = getimagesize($tmp_name); 
       echo $width;
       echo $height;
    }