-2

While working on my photo gallery I decided it was best to have an alternate file if image that should display doesn't. I've looked on this site, and may others. They all say to use:

$VariableName = "photography/small/2014-09-21-red-1.png";
if (file_exists ($VariableName)) {echo "Yes!!!";}
else {echo "Nooo!!!";}

or

if (file_exists ("photography/small/2014-09-21-red-1.png")) {echo "Yes!!!";}
else {echo "Nooo!!!";}

For some reason, this will not work for me. It does work, but only when file_exists is set to !file_exists, which is saying: "if this file does not exist, display the image that exists (the image I want, not it's replacement)". In other words: this is saying if apple exists, display "orange"; and if apple does not exist, display apple.

I've even placed the generated image link in the search bar (when using !file_exists), and after pressing enter, it brings me to the image. I've made sure that the images are set to 0777 in case that's interfering, but it seems to have no effect. All of the $DataRows variables are connected to a database and I've triple-checked that the file names in photography/small match those in the database table.

Why is this happening?

$URLPath = "http://localhost/~matthew/";
if (file_exists ($URLPath . "photography/small/" . $DataRows["DatePublished"] . "-" . $DataRows["FileName"] . "." . $DataRows["ImageExtension"])) {
 echo '<img src="' . $URLPath . "photography/small/" . $DataRows["DatePublished"] . "-" . $DataRows["FileName"] . "." . $DataRows["ImageExtension"] . '" alt="' . $DataRows["PhotoName"] . '">' . "\n";
}
else {
 echo '<img src="' . $URLPath . 'img/no-image.png" alt="Image Not Here">' . "\n";
}

Thank you very much for the help.

Matthew Campbell
  • 1,118
  • 14
  • 25
  • is ~mathew a folder in the htdocs folder? – Teddy Codes Aug 14 '16 at 20:03
  • Any specific reason, why you check the file existence via the detour of HTTPing into your own server? That is, why `http://localhost/~matthew` and not, e.g., `/var/www/my-docroot`? – Boldewyn Aug 14 '16 at 20:06
  • RTM http://php.net/manual/en/function.file-exists.php *"As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality."* – Funk Forty Niner Aug 14 '16 at 20:08
  • @Robert I believe `~matthew` is a `htdocs` folder. I'm using a Mac as a server, which is set up differently from Windows and Linux. – Matthew Campbell Aug 14 '16 at 21:12
  • @Boldewyn I'm using Mac as a server, which doesn't have the `www` folder like Windows has. – Matthew Campbell Aug 14 '16 at 21:14

4 Answers4

1

It looks like you are checking for a file but passing a URL into the function. It is probably returning false since the URL is not a valid path on your server. I would suggest using the actual path of the file or if you have to use a URL check out this post: How to check if a file exists from a url

Community
  • 1
  • 1
pucky124
  • 1,489
  • 12
  • 24
  • I would, but I'm using a database loop for each entry. As I said in my post, I've triple-checked the file names in both the folder and the database table. All are correct. – Matthew Campbell Aug 14 '16 at 20:14
1

What happens if you change $URLPath from

$URLPath = "http://localhost/~matthew/";

to

$URLPath = $_SERVER['DOCUMENT_ROOT'] . "/~matthew/";
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • It returns `Library/WebServer/Documents/~matthew/`, which leads to nothing. – Matthew Campbell Aug 14 '16 at 20:22
  • Odd. I never use URLs for queries to skip the ping. Does it give the same result using is_file() rather than file_exists() ? Are you sure your db is OK? –  Aug 14 '16 at 20:32
  • I'm positive the database is okay. I also have a nav menu and mini nav-menu that use a database, and they display without any problem. `is_file()` also returns false. – Matthew Campbell Aug 14 '16 at 20:39
  • Than I assume ~matthew is an alias rather than a physical folder. If so, check your server's config to get the proper fs path. Good luck. –  Aug 14 '16 at 20:58
0

Try using clearstatcache() and double check the file names and paths.

CraftyScoundrel
  • 144
  • 1
  • 6
0

if (file_exists ($VariableName or "path-to-image")) {

This is gibberish. As long as "path-to-image" is not null, the operand will evaluate as (bool) true.

It should be:

if (file_exists ($VariableName)) {

It looks like you should be using

If(is_readable($variable)) {

But looking at your later code you are not using the construct you originally quoted.

file_exists() works as described for me in lots of different contexts.

symcbean
  • 47,736
  • 6
  • 59
  • 94
  • the `$VariableName or "path-to-image"` was placed together to show you could use `if (file_exists ($VariableName)` or `if (file_exists ("path-to-image")`. I'll fix it above. – Matthew Campbell Aug 14 '16 at 20:25
  • @symcbean I was going to make a comment about their use of `if (file_exists ($VariableName or "path-to-image"))` but then realized that it was what the OP just stated now. The code example should have been better worded. I figured out what they meant after reading it over a couple of times, soon as that question was posted. There were comments posted under the questions, and were not answered. So, it's anybody's ballgame right now. I'm suspecting it's the way they're using the path altogether. Their example of `$VariableName = "path-to-imagepng";` is quite vague, IMHO. – Funk Forty Niner Aug 14 '16 at 20:33