php - 警告:filesize(): stat failed for /images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png
问题描述
我想获取图像的大小,但我有一个警告问题我有这个代码
$html = file_get_contents('https://www.google.com');
$dom = new domDocument;
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$file= $image->getAttribute('src');
echo filesize($file);
}***
解决方案
您可以使用以下内容:
<?php
$url = 'https://www.google.com';
$html = file_get_contents($url);
$dom = new domDocument;
@$dom->loadHTML($html);
$dom->preserveWhiteSpace = false;
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
$file= $image->getAttribute('src');
$img = get_headers($url . $file, 1);
echo 'image size : ' . $img["Content-Length"];
}
- 用于
get_headers()
获取服务器响应 HTTP 请求php.net/get_headers()发送的所有标头
推荐阅读
- python-3.x - TypeError:无法腌制“_thread.lock”对象。在将 ANTLR 解析器对象转储到 pickle 时
- json - 在单个对象下连接多个对象并使用 jq -f .jq 添加同级对象
- java - Itext 和 lowagie 停止工作 - NoSuchMethod getCharBBox(char)
- sql - Jpa 无法识别 WHERE 条件
- c++ - cmake 突然不适用于更新的 Xcode
- javascript - 如何在附加项目中添加关闭按钮?
- flutter - 在 TextField() 中为什么我们使用“onSubmitted:”语法?
- r - 在R中双重积分
- command-line - Thunderbird 使用命令行回复
- javascript - 使用管道过滤器在 Ionic 中的搜索栏