php php如何以kb为单位获取Web图像大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6272663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:51:48  来源:igfitidea点击:

php how to get web image size in kb?

phpimageimage-size

提问by fish man

php how to get web image size in kb?

php如何以kb为单位获取Web图像大小?

getimagesizeonly get the width and height.

getimagesize只获取宽度和高度。

and filesizecaused waring.

filesize造成waring.

$imgsize=filesize("http://static.adzerk.net/Advertisers/2564.jpg");
echo $imgsize;

Warning: filesize() [function.filesize]: stat failed for http://static.adzerk.net/Advertisers/2564.jpg

Warning: filesize() [function.filesize]: stat failed for http://static.adzerk.net/Advertisers/2564.jpg

Is there any other way to get a web image size in kb?

有没有其他方法可以以 kb 为单位获取网络图像大小?

回答by mario

Short of doing a complete HTTP request, there is no easy way:

没有做一个完整的 HTTP 请求,没有简单的方法:

$img = get_headers("http://static.adzerk.net/Advertisers/2564.jpg", 1);
print $img["Content-Length"];

You can likely utilize cURLhowever to send a lighter HEADrequest instead.

cURL然而,您可能会利用来发送更轻的HEAD请求

回答by Morgan Delaney

<?php
$file_size = filesize($_SERVER['DOCUMENT_ROOT']."/Advertisers/2564.jpg"); // Get file size in bytes
$file_size = $file_size / 1024; // Get file size in KB
echo $file_size; // Echo file size
?>

回答by Nicholas Kreidberg

That sounds like a permissions issue because filesize() should work just fine.

这听起来像是权限问题,因为 filesize() 应该可以正常工作。

Here is an example:

下面是一个例子:

php > echo filesize("./9832712.jpg");
1433719

Make sure the permissions are set correctly on the image and that the path is also correct. You will need to apply some math to convert from bytes to KB but after doing that you should be in good shape!

确保在图像上正确设置了权限并且路径也正确。您将需要应用一些数学运算才能将字节转换为 KB,但这样做之后您应该处于良好状态!

回答by Nick Pyett

Not sure about using filesize()for remote files, but there are good snippets on php.net though about using cURL.

不确定filesize()用于远程文件,但在 php.net 上有关于使用 cURL 的很好的片段。

http://www.php.net/manual/en/function.filesize.php#92462

http://www.php.net/manual/en/function.filesize.php#92462

回答by Sandeep Tawaniya

You can use also this function

您也可以使用此功能

<?php
$filesize=file_get_size($dir.'/'.$ff);
$filesize=$filesize/1024;// to convert in KB
echo $filesize;


function file_get_size($file) {
    //open file
    $fh = fopen($file, "r");
    //declare some variables
    $size = "0";
    $char = "";
    //set file pointer to 0; I'm a little bit paranoid, you can remove this
    fseek($fh, 0, SEEK_SET);
    //set multiplicator to zero
    $count = 0;
    while (true) {
        //jump 1 MB forward in file
        fseek($fh, 1048576, SEEK_CUR);
        //check if we actually left the file
        if (($char = fgetc($fh)) !== false) {
            //if not, go on
            $count ++;
        } else {
            //else jump back where we were before leaving and exit loop
            fseek($fh, -1048576, SEEK_CUR);
            break;
        }
    }
    //we could make $count jumps, so the file is at least $count * 1.000001 MB large
    //1048577 because we jump 1 MB and fgetc goes 1 B forward too
    $size = bcmul("1048577", $count);
    //now count the last few bytes; they're always less than 1048576 so it's quite fast
    $fine = 0;
    while(false !== ($char = fgetc($fh))) {
        $fine ++;
    }
    //and add them
    $size = bcadd($size, $fine);
    fclose($fh);
    return $size;
}
?>

回答by lockdown

Here is a good link regarding filesize()

这是一个关于 filesize() 的好链接

You cannot use filesize() to retrieve remote file information. It must first be downloaded or determined by another method

您不能使用 filesize() 来检索远程文件信息。必须先下载或通过其他方法确定

Using Curl here is a good method:

在这里使用 Curl 是一个很好的方法:

Tutorial

教程

回答by Ahmad Vaqas Khan

You can get the file size by using the get_headers() function. Use below code:

您可以使用 get_headers() 函数获取文件大小。使用以下代码:

    $image = get_headers($url, 1);
    $bytes = $image["Content-Length"];
    $mb = $bytes/(1024 * 1024);
    echo number_format($mb,2) . " MB";