小编the*_*ant的帖子

使用 file_exists() 检查 WordPress 上传文件夹

我在 WordPress 上传文件夹中创建了一个目录,供最终用户通过 ftp 批量上传照片。图像编号为 1.jpg、2.jpg...等。我已成功生成图像网址,但现在我想测试空网址 - 即如果“8.jpg”不存在,则显示占位符图像而是从主题的图像文件夹中。

我正在尝试使用 file_exists(),但这每次都会返回 false 并且始终显示占位符图像。任何建议,将不胜感激。

<?php while ( have_posts() ) : the_post();
    // create url to image in wordpress 'uploads/catalogue_images/$sale' folder
    $upload_dir = wp_upload_dir();                          
    $sub_dir = $wp_query->queried_object;
    $image = get_field('file_number');
    $image_url = $upload_dir['baseurl'] . "/catalogue_images/" . $sub_dir->name . "/" . $image . ".JPG"; ?>

    <?php if(file_exists($image_url)){
        echo '<img src="' . $image_url . '" alt="" />';
    } else {
        //placeholder
        echo '<img src="' . get_bloginfo("template_url") . '/images/photo_unavailable.jpg" alt="" />';
    } ?>                                
<?php endwhile; ?>
Run Code Online (Sandbox Code Playgroud)

php wordpress file-exists

2
推荐指数
1
解决办法
8168
查看次数

标签 统计

file-exists ×1

php ×1

wordpress ×1