PHP file_exists not working

software development


So I’ve got some code. What it does is search through a given directory for PNG files, and then displays them in a table by running them them through another image-resize script. Here it is in action

What I’m trying to do is check to see if a file by the same name exists, but with a GIF extension (at 24x24 for use on DigiChat environments). If it does exist, a small link should be added.

So inside the while loop which cycles through files and the if loop which makes sure to discard any non-PNG results, I have this:

[code] if ($i==1) { echo “

\n”; }
$diginame = substr($files[$c], 0, strrpos($files[$c], ‘.’));
$digifull = $icondir.’/’.$icontype.’/’.$diginame.’.gif’;

if (ereg(’~’, $diginame)) {
$fullname = substr($diginame, strpos($diginame, ‘~’)+1);
} else {
$fullname = $diginame;
$wikiname = ereg_replace(" “, “_”, $fullname);
echo “

<img src=”$icondir/$icontype/reboth.php?h=$iconsize&w=$iconsize&img=$files[$c]”>
<a href=“$wikiname”>$fullname";

if ( file_exists($digifull) ) {
echo “<a href=”$digifull">[D]";
// echo “
echo “

if ($i==6) { echo “\n”; $i=1;}
$diginame up near the beginning is the same as the PNG filename, but with “.png” stripped off.

$digifull is the full path to the theoretical GIF file.

So I’ve got if (file_exists($digifull)) { } going on, but it’s always returning FALSE. When I comment it out and just echo $digifull, I get the correct full path and filename. If I just echo the link to the file, the link works fine.

I don’t have any .htaccess redirects going and I can’t figure out why it’s returning false when these files are clearly there. Anyone have any idea what I’m doing wrong? Knowing me, it’s something blindingly obvious.

Thanks in advance.


php is looking at the operating system location of the file, not the web location. that’s the most common confusion people run into.

specifically, if the file’s at, you can link to /images/somefile.gif but file_exists is going to look at the operating system for /images/somefile.gif and not find it, because it’s actually somewhere like /home/username/ there.

you can get the /home/username/ part from $_SERVER[‘DOCUMENT_ROOT’]

track7 - my dream-hosted site


Fantastic, that worked like a charm. The documentation was decidedly unhelpful on that. Thanks very much!