简体   繁体   English

如何在将上传的文件保存到服务器之前使用getImagesize()?

[英]How to use getImagesize() for an uploaded file before it gets saved into the server?

Given that getImagesize() requires a string and $_FILES['myfile'] is an array, I've tried to get to the temporary folder and the file uploaded there in order to get the image size, but I do get an error. 鉴于getImagesize()需要一个字符串并且$_FILES['myfile']是一个数组,我试图进入临时文件夹并将文件上传到那里以获取图像大小,但是我确实得到了一个错误。

This is the error that I get: 这是我得到的错误:

Warning: getimagesize(/home/everymorning/public_html/tmp/phpNFuAMD/images.jpeg): failed to open stream: No such file or directory in /home/everymorning/public_html/demo1.php on line 48 警告:getimagesize(/home/everymorning/public_html/tmp/phpNFuAMD/images.jpeg):无法打开流:第48行的/home/everymorning/public_html/demo1.php中没有此类文件或目录

And here is what I've tried: 这是我尝试过的:

$string = $_SERVER['DOCUMENT_ROOT'].$_FILES['myfile']['tmp_name'].'/'.$_FILES['myfile']['name'];

echo getimagesize($string);

And if I try to use the temporary path directly , I still get an error: 而且, 如果我尝试直接使用临时路径 ,仍然会收到错误消息:

If I use: 如果我使用:

getimagesize($_FILES['myfile']['tmp_name']);

I get: 我得到:

Notice: Array to string conversion in /home/everymorning/public_html/demo1.php on line 47 注意:第47行的/home/everymorning/public_html/demo1.php中的数组到字符串的转换

I think getimagesize($fileName) has following things- 我认为getimagesize($fileName)具有以下内容-

Array ( [0] => 667 
        [1] => 184 
        [2] => 3 
        [3] => width="667" height="184" 
        [bits] => 8 
        [mime] => image/png );

You are trying to echo an array- error of Array to string conversion . 您正在尝试将Array to string conversion的array-error错误回显Array to string conversion Infact echo could not print array instead print_r and var_dump are the those can print the info of array. 实际的echo无法打印数组,而print_rvar_dump是可以打印数组的信息。

To calculate the size of an image you can simply calculate by multiplying height width and bits used to represent a pixel and following arithmetic expression can give you size of an image in bits. 要计算图像的大小,您可以简单地通过将高度宽度与用于表示像素的位相乘来进行计算,以下算术表达式可以为您提供图像的大小(以位为单位)。

 $imageInfo = getimagesize($fileName);
 $size = $imageInfo[0] * $imageInfo[1] * $imageInfo['bits'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM