0
我在image.php文件下面的代碼獲取圖像文件的大小,如果它不是直接鏈接
<?php
if ($_GET['id'] == 1)
echo '<img src="Koala.jpg">';
?>
我怎樣才能得到圖像大小以KB爲單位,如果我只知道網址:http://localhost/test/image.php?id=1
我在image.php文件下面的代碼獲取圖像文件的大小,如果它不是直接鏈接
<?php
if ($_GET['id'] == 1)
echo '<img src="Koala.jpg">';
?>
我怎樣才能得到圖像大小以KB爲單位,如果我只知道網址:http://localhost/test/image.php?id=1
您可以使用filesize()
來獲取圖像的文件大小。
返回文件的大小(以字節爲單位)或FALSE(並生成E_WARNING級別的錯誤)以防萬一發生錯誤。
$filename = "Koala.jpg";
$size = filesize($filename) . ' B';
$size = number_format($size/1024, 2) . ' KB';
這將在千回的圖像文件的大小。
如果你只知道網址,你可以使用get_headers()
:從http://www.w3bees.com/2013/03/get-remote-file-size-using-php.html引用
function remote_file_size($url){
$data = get_headers($url, true);
if (isset($data['Content-Length']))
return (int) $data['Content-Length'];
}
echo remote_file_size('http://example.com/image.png');
方法。
但我只知道網址 – Barba