• I’m trying to retrieve images for posts, which are held in a main directory, using if (file_exists() but it’s not recognising the file path.

    For each post, there are up to 8 images available. Each image has the letters a-g at the end of the file name (or nothing), and has str_replace to replace certain characters in the file names.

    I need to show every image if it exists, and if not, to return nothing. So if the post is associated with images with b, d and f at the end, it just shows those three.

    I’ve tested without the (file_exists() and its able to pick up the images with a simple echo for each – but it seems the $img paths aren’t being recognised.

    Any help would be appreciated…

    $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 ""; }
  • The topic ‘If (file_exists() – not showing file’ is closed to new replies.