如何获取远程存储图像的文件大小? (php) [英] How to get the file size of a remotely stored image? (php)

查看:495
本文介绍了如何获取远程存储图像的文件大小? (php)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个存储在远程服务器中的图像文件(例如,以请参阅此处),但这不适用于远程文件(请参见此处).

另一种选择是检查内容长度",但是我认为它不适用于图像文件(这里(例如:

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

但无需下载图像.有可能吗?

解决方案

假设您担心文件的大小(而不是图像的大小),则可以使用Content-Length,它通常会起作用./p>

如果另一端的服务器没有提供标头,则别无选择,只能获取文件并在本地检查其大小.

<?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";

Let's say we have an image file, stored in a remote server (for example, let's take this image), how can we determine (in PHP code) it's file size?

If the file was on server, we would have used filesize (see here), but this wouldn't work on a remote file (see here).

The other alternative is to check for the "Content-Length", but I believe it wouldn't work for an image file (see here)

I would like for a solution like the one given here (e.g, something like:

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

But without the need to download the image. Is that possible?

解决方案

Assuming you're worried about the size of the file (not the dimensions of the image), you can grab Content-Length and it will usually work.

If the server on the other end does't supply the header, you'll have no choice but to GET the file, and check its size locally.

<?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";

这篇关于如何获取远程存储图像的文件大小? (php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆