首页 > 解决方案 > 如何检查图像是否存在于 wodpress 附件中

问题描述

当我上传一个非常小的 50x50px 图像时,它只是作为原始文件(全尺寸)。如何检查图像是否存在。谢谢您的帮助。

if ($image_size == 'thumbnail'):
  $image_src = $image['sizes']['thumbnail'];
  $image_lazy = $image_src
if ($image_size == 'medium'):
  $image_src = $image['sizes']['medium'];
  $image_lazy = $image_src;
else: 
  $image_lazy = $image_src; //oryginal size ???
endif;

标签: phpwordpressimagelazy-loading

解决方案


要检查 PHP 中是否存在文件,Php 已经给了我们函数file_exists(string $filename): bool

<?php
$filename = '/path/to/yourfile.jpg';

if (file_exists($filename)) {
    echo "The file $filename exists";
} else {
    echo "The file $filename does not exist";
}
?>

推荐阅读