【问题标题】:Why is file_exists() returning false?为什么 file_exists() 返回 false?
【发布时间】:2025-12-09 23:45:01
【问题描述】:

我正在尝试使用if (file_exists() 检索保存在 wp-uploads 目录中的 WP 帖子的图像,但它无法识别文件路径。

对于每个帖子,最多有 8 张图片可用。每个图像的文件名末尾都有字母 a-g(或没有),并有 str_replace 来替换文件名中的某些字符。

如果存在,我需要显示每个图像,如果不存在,则不返回任何内容。因此,如果帖子与末尾带有 b、d 和 f 的图像相关联,它只会显示这三个。

我已经在不使用 (file_exists()) 的情况下进行了测试,它能够通过简单的回声为每个图像拾取图像 - 但似乎无法识别 $img 路径。

我有点 php 菜鸟,所以任何帮助将不胜感激......

$uid = get_post_meta (get_the_ID(), 'Unique number', true);
$root ="/wp-content/uploads/2016/Collection/";
$path = str_replace(" ","_",$uid);
$path = str_replace(".","_",$path);
$path = str_replace(":","",$path);

$img = $root.$path.".jpg";
$imga = $root.$path."a.jpg";
$imgb = $root.$path."b.jpg";
$imgc = $root.$path."c.jpg";
$imgd = $root.$path."d.jpg";
$imge = $root.$path."e.jpg";
$imgf = $root.$path."f.jpg";
$imgg = $root.$path."g.jpg";

if (file_exists($img)) { echo "<img src='".$root.$path.".jpg' />"; } else { echo ""; }
if (file_exists($imga)) { echo "<img src='".$root.$path.".jpg' />"; } else { echo ""; }
if (file_exists($imgb)) { echo "<img src='".$root.$path."b.jpg' />"; } else { echo ""; }
if (file_exists($imgc)) { echo "<img src='".$root.$path."c.jpg' />"; } else { echo ""; }
if (file_exists($imgd)) { echo "<img src='".$root.$path."d.jpg' />"; } else { echo ""; }
if (file_exists($imge)) { echo "<img src='".$root.$path."e.jpg' />"; } else { echo ""; }
if (file_exists($imgf)) { echo "<img src='".$root.$path."f.jpg' />"; } else { echo ""; }
if (file_exists($imgg)) { echo "<img src='".$root.$path."g.jpg' />"; } else { echo ""; }`

【问题讨论】:

    标签: php wordpress file-exists


    【解决方案1】:

    您需要重新安排告诉 PHP 查找地址的方式,

    $root 可能不是您的绝对文件路径根(可能意味着绝对),因此请为此使用特殊的超级变量 $_SERVER['DOCUMENT_ROOT'],它是 Web 可访问文件路径的根,因此然后你有:

    $img = $_SERVER['DOCUMENT_ROOT'].$root.path.".jpg"
    //while retaining your current / at the start of $root
    

    这是检查文件是否存在的文件结构不是要在&lt;img&gt; 标记中引用的文件结构,这在您的示例中似乎是正确的更多。

    所以,你的整体修正应该是这样的:

    $root ="/wp-content/uploads/2016/Collection/";
    $path = str_replace(" ","_",$uid);
    $path = str_replace(".","_",$path);
    $path = str_replace(":","",$path);
    
    $img = $root.$path.".jpg";
    ...
    if (file_exists($_SERVER['DOCUMENT_ROOT'].$img)){
    ....
    }
    

    另外需要注意的是,这个函数的结果是缓存的,所以你应该在这个文件的开头调用clearstatcache(),这样它就可以重新检查图像是否存在。目前没有这个,即使图像确实存在,PHP 将使用缓存的 - 过去的结果,这可能不是最新的。

    【讨论】:

    • 是的,您必须使用完整或绝对文件路径,如果这不适合您,请确保它仅在您的 PHP 配置中激活 allow_url_fopen 时才有效。
    • $_SERVER['DOCUMENT_ROOT'] 为我解决了这个问题 - 谢谢。
    【解决方案2】:

    您以/ 开头$root,因此它从服务器的根目录开始。删除第一个 / 并重试。

    【讨论】:

    • 虽然这是 a 解决方案,但它并不是最好的解决方案,因为它非常广泛地假设图像的文件路径是运行的 PHP 脚本的子路径,因此,如果 PHP 脚本位于 /wp-content/admin/ 文件夹中,例如,此解决方案将找不到使用 $root 指定的文件。
    • 你是对的,但我没有所有的信息,所以我猜测文档根目录在 WordPress 的根目录下。
    【解决方案3】:
    $uid = get_post_meta (get_the_ID(), 'Unique number', true);
    $path = str_replace(" ","_",$uid);
    $path = str_replace(".","_",$path);
    $path = str_replace(":","",$path);
    
    $uploads = wp_upload_dir();
    

    获取基本根目录。

    $root = $uploads['path'];
    $imgDir = $root.$path.".jpg";
    
    if (file_exists($imgDir)){
    .......
    }
    

    【讨论】:

    • 检查来自 C:\development\xampp\htdocs\example.com/content/uploads/2016/05
    • 也许将此数据添加到您的答案中以使您的答案更有用