如何获取远程存储图像的文件大小? (php)

发布于 2024-09-27 05:14:46 字数 929 浏览 1 评论 0 原文

假设我们有一个图像文件,存储在远程服务器中(例如,让我们采用 此图像),我们如何确定(在 PHP 代码中)它的文件大小?

如果文件位于服务器上,我们将使用文件大小(请参阅此处),但这不适用于远程文件(请参阅此处 )。

另一种选择是检查“内容长度”,但我相信它不适用于图像文件(请参阅此处

我想要一种像给定的解决方案此处(例如,类似:

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

但不需要下载图像。这可能吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

千と千尋 2024-10-04 05:14:46

假设您担心文件的大小(而不是图像的尺寸),您可以获取 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";

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";
美羊羊 2024-10-04 05:14:46

echo get_headers($url,1)['内容长度'];

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

梦毁影碎の 2024-10-04 05:14:46

其他人有一个函数,基本上将其作为套接字连接:
http://snippets.dzone.com/posts/show/1207

Someone else has a function for that that basically does it as a socket connection:
http://snippets.dzone.com/posts/show/1207

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文