2010-10-08 77 views
3

比方說,我們有一個圖像文件,存儲在遠程服務器(例如,讓我們this image),我們如何才能確定(在PHP代碼),這是文件的大小?如何獲得遠程存儲的圖像文件的大小? (PHP)

如果文件位於服務器上,我們將使用文件大小(see here),但這不適用於遠程文件(see here)。

另一種方法是檢查的「內容長度」,但我相信這不會對圖像文件進行操作(see here

我想對於像給here的一個解決方案(例如, ,是這樣的:。

<?php 
function get_remote_size($url) { // magic 
} 
echo get_remote_size("http://humus101.com/wp-content/uploads/2009/11/Hummus-soup.jpg"); 
?> 

但無需下載圖像是可能的

回答

8

假設你擔心文件的大小(而不是圖像的尺寸),你可以抓取Content-Length,它通常會工作。

如果在另一端服務器簡化版,提供標題,你別無選擇,只能獲取文件,並在本地檢查它的大小。

<?PHP 
$headers = get_headers('http://humus101.com/wp-content/uploads/2009/11/Hummus-soup.jpg'); 
$size = null; 
foreach($headers as $h){ 
    /** look for Content-Length, and stick it in $size **/ 
} 
if ($size === null){ //we didn't get a Content-Length header 
    /** Grab file to local disk and use filesize() to set $size **/ 
} 

echo "image is $size bytes"; 
+0

我使用[和getimagesize(http://php.net/manual/en/function.getimagesize.php),但它在下載得到的圖像尺寸前的整個文件。感謝這一個。 – machineaddict 2013-04-16 07:42:26

0

別人已經針對基本上做它作爲一個套接字連接功能: http://snippets.dzone.com/posts/show/1207

+0

感謝。它是一個套接字連接的優點是什麼? – 2010-10-08 22:19:06

+0

它允許它做一個HEAD請求回PHP4它不具有'get_headers'。我今天不會使用它。 – bobince 2010-10-08 23:48:26

+0

它會,但它下載整個文件。 – machineaddict 2013-04-16 07:33:01

1

echo get_headers('url,1)['Content-Length'];

相關問題