Aitch Aitch - 1 year ago 64
PHP Question

Why is file_exists() returning false?

I'm trying to retrieve images for WP posts, which are held in a wp-uploads 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 each 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
and its able to pick up the images with a simple echo for each - but it seems the
paths aren't being recognised.

Im a bit of a php noob so 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 ""; }`

Answer Source

You need to rearrange the way you are telling PHP to look up the address,

$root is probably not your absolute file path root (probably meaning absolutely) so instead use the special super variable for this, $_SERVER['DOCUMENT_ROOT'] which is the root of the web accessible filepath, therefore you then have:

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

This is the file structure to check if the file exists, not the file structure to reference in the <img> tag, that appears to be correct in your examples above.

So, your overall correction should look like this:

$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)){

An additional note is that the results of this function are cached so you should call clearstatcache() at the start of this file so that it can make fresh checks for if the images exist. Currently without this even if the image does exist, PHP will be using the cached - past- results which may not be up to date.