Hello everyone,
After trying and trying and trying, I’ve finally decided to ask for help. I am attempting to check to see if a file exist, and if so, do one action, and if not, do another. Simple right? Well that’s what I thought.
I run an automotive site, and would like the header image to change, based on the make of the car the client is interested in. For example, if you are looking at Ford.html, you will see a Ford.jpg in the header. If you are looking at Chevy.html, you will see Chevy.jpg in the header, etc... I use [*alias*] for the filename, some keywords here and there, and for the name of the jpeg as well.
So, on page Ford.html, you will see a Ford.jpg, and the word Ford scattered throughout the page. Also, Ford.jpg is a file located in /assets/images/Ford.jpg. Make sense?
Okay, here is what I got so far.
In my header template I have [[CarPic]].
And here is the snippet I created called CarPic.
$filename = '/assets/images/[*alias*].jpg';
if (file_exists($filename)) {
echo "file". $filename. " exists";
echo "<img src=\"$filename\" alt=\"[*alias*] car pic\">";
} else {
echo $filename. " does NOT exist";
echo "<img src=\"/assets/images/Lexus.jpg\" alt=\"car pic\">";
}
Obviously, I hope, I want to show a picture of whatever car is relevent. If nothing is relevent, I’ll show a picture of a Lexus. Well, no matter what I do, the above code ALWAYS shows Lexus.jpg. Or, file_exists, always returns false. Ugh!
So, when I use the above code I see a Lexus on every page. If I reverse the statements (exists and does not exist), I see exactly what I want on Ford.html and Chevy.html etc... But, if no picture exists (i.e. home.html), I see nothing.
Notice for debugging purposes I am echoing $filename everytime. $filename echos correctly, returning /assets/images/Ford.jpg or whatever.
Funny thing, if I run the code exactly as I have it above and visit my Ford.html page, I will see "/assets/images/Ford.jpg does NOT exist" then a picture of a Lexus. If I copy and past "/assets/images/Ford.jpg" into my URL, I will see the picture of the Ford! LOL.
For fun, or if it helps, you can see all this stuff in action at
denvertransmissions.com. The site is far from done, but you’ll get the idea of what I’m talking about. Down at the bottom of every page you will see "Transmission Repair By Make" and a list of a bunch of cars. I have only done Ford, Dodge, and Jeep so far.