2010-10-08 76 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'];

相关问题