首页 > 解决方案 > 警告: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

解决方案


您可以使用以下内容:

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

推荐阅读