php - 使用 PHP 计算远程图像文件大小

标签 php

如何在远程服务器上找到图像的文件大小?在本地机器上,我可以使用 filesize() ,但我想在不下载图像的情况下找到大小。

最佳答案

// Similar solution seen in another stackoverflow post:

// By default get_headers uses a GET request to fetch the headers. If you
// want to send a HEAD request instead, you can do so using a stream context:
stream_context_set_default(
    array(
        'http' => array(
            'method' => 'HEAD'
        )
    )
);

$headers = get_headers($source_file,1);
$source_file_size = null;
if (is_array($headers) && array_key_exists('Content-Length',$headers)) {
    $source_file_size = intval($headers['Content-Length']);
}
if ($source_file_size === null){ //we didn't get a Content-Length header
    /** Grab file to local disk and use filesize() to set $size **/
}

stream_context_set_default(
    array(
        'http' => array(
            'method' => 'GET'
        )
    )
);

关于php - 使用 PHP 计算远程图像文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8153609/

相关文章:

php - 使用php在数据库中存储点击次数

php - 在 Yii2 中访问数据

php - WordPress async-upload.php 403 错误

php - 当模式无效时,如何禁用 DOMDocument::schemaValidate() 产生的警告?

php - Magento:为后端操作生成 url(带键)

php - 扩展类时的构造函数注入(inject)

php - 使用 php mysql 插入数据和数组值

php - 从父子中计算子级 php mysql 5 级深

php - PDO::prepare() 如何生成 PDOStatement 对象?

php - 使用 MySQL 进行简单的统计页面排名