我正在尝试获取远程图像文件大小。
但我收到此错误: "警告:filesize():http://mfastore.com/_store/user_data/9fc8022_310/C/hydrangeas.jpg的统计信息失败 文件大小未知 "
这是我试图以MB为单位获取图像大小的代码
$res= formatFilebytes("http://mfastore.com/_store/user_data/9fc8022_310/C/hydrangeas.jpg","MB");
echo $res;
function formatFilebytes($file, $type)
{
switch($type){
case "KB":
$filesize = filesize($file) * .0009765625; // bytes to KB
break;
case "MB":
$filesize = (filesize($file) * .0009765625) * .0009765625; // bytes to MB
break;
case "GB":
$filesize = ((filesize($file) * .0009765625) * .0009765625) * .0009765625; // bytes to GB
break;
}
if($filesize <= 0){
return $filesize = 'unknown file size';}
else{return round($filesize, 2).' '.$type;}
}
请帮助我。
感谢
答案 0 :(得分:1)
试试这个
$image_header = get_headers("http://mfastore.com/_store/user_data/9fc8022_310/C/hydrangeas.jpg", true);
$size = $image_header["Content-Length"];
答案 1 :(得分:0)
这个解决方案对我有用......
$url = 'http://mfastore.com/_store/user_data/9fc8022_310/C/hydrangeas.jpg';
echo getRemoteFilesize($url);
function getRemoteFilesize($url, $formatSize = true)
{
$head = array_change_key_case(get_headers($url, 1));
// content-length of download (in bytes), read from Content-Length: field
$clen = isset($head['content-length']) ? $head['content-length'] : 0;
// cannot retrieve file size, return "-1"
if (!$clen) {
return -1;
}
if (!$formatSize) {
return $clen; // return size in bytes
}
$size = $clen;
switch ($clen) {
case $clen < 1024:
$size = $clen .' B'; break;
case $clen < 1048576:
$size = round($clen / 1024, 2) .' KiB'; break;
case $clen < 1073741824:
$size = round($clen / 1048576, 2) . ' MiB'; break;
case $clen < 1099511627776:
$size = round($clen / 1073741824, 2) . ' GiB'; break;
}
return $size; // return formatted size
}
答案 2 :(得分:0)
我相信这会对你有所帮助,这是一个例子:
$filesize = filesize($folder_url . $file) * .0009765625; // bytes to KB