php如何获得以kb为单位的web图像大小?
php如何获得以kb为单位的web图像大小php如何获得以kb为单位的web图像大小?,php,image,image-size,Php,Image,Image Size,php如何获得以kb为单位的web图像大小 getimagesize仅获取宽度和高度 和filesize导致waring $imgsize=filesize("http://static.adzerk.net/Advertisers/2564.jpg"); echo $imgsize; 警告:filesize()[function.filesize]:的stat失败http://static.adzerk.net/Advertisers/2564.jpg 有没有其他方法可以获得以kb为单位的w
getimagesize
仅获取宽度和高度
和filesize
导致waring
$imgsize=filesize("http://static.adzerk.net/Advertisers/2564.jpg");
echo $imgsize;
警告:filesize()[function.filesize]:的stat失败http://static.adzerk.net/Advertisers/2564.jpg
有没有其他方法可以获得以kb为单位的web图像大小?这听起来像是权限问题,因为filesize()应该可以正常工作 以下是一个例子:
php > echo filesize("./9832712.jpg");
1433719
确保在映像上正确设置了权限,并且路径也正确。您将需要应用一些数学来将字节转换为KB,但在这样做之后,您应该处于良好状态
<?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
?>
这里有一个关于filesize()的好链接
不能使用filesize()检索远程文件信息。必须首先下载或通过其他方法确定
在这里使用Curl是一种很好的方法:
除了执行完整的HTTP请求,没有简单的方法:
$img = get_headers("http://static.adzerk.net/Advertisers/2564.jpg", 1);
print $img["Content-Length"];
但是,您可能可以利用发送一个。不确定是否要对远程文件使用
filesize()
,但是php.net上有一些关于使用cURL的好片段
您也可以使用此功能
<?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;
}
?>
您可以使用get_headers()函数获取文件大小。使用以下代码:
$image = get_headers($url, 1);
$bytes = $image["Content-Length"];
$mb = $bytes/(1024 * 1024);
echo number_format($mb,2) . " MB";
可能与此相关:[link]太好了,让_头运行得更快。谢谢。请确保您的HTTP客户端没有发送任何表示它接受Gzip HTTP响应的头,否则内容长度将是错误的,因为服务器将返回压缩数据。@Darien:捕获得很好!幸运的是,get_headers
发送了一个非常简单的HTTP/1.0请求。但是对于cURL,这需要更多的努力。get\u headers
也可以执行HEAD请求。php手册中有一个例子说明了如何操作。它有时会返回一个数组:array([0]=>0[1]=>103823)
如何处理<代码>获取标题($imageURL,1)[“内容长度”]
其中$imageURL
是