file_exists can't find the file on a webserver

Post Reply
User avatar
battlethem
Jr. Bludit
Posts: 4
Joined: Sun Dec 09, 2018 7:52 pm

Thu Dec 13, 2018 7:10 pm

Hey!
I'm making a theme and want to insert a simple 'if' statement for the image.
Basically, if there's a file "image.jpg" in "my_theme/img/", it'll echo 'Apples', otherwise - 'Oranges':

Code: Select all

<!--IMAGE EXISTS-->
<?php if (file_exists('bl-themes/my_theme/img/image.jpg')): ?> 
<?php echo "Apples" ?>
<?php endif ?>

<!--IMAGE DOESN'T EXIST-->
<?php if (!file_exists('bl-themes/my_theme/img/image.jpg')): ?>
<?php echo "Oranges" ?>
<?php endif ?>
The php file is here:
folder/bl-themes/my_theme/php/home.php
The image is here:
folder/bl-themes/my_theme/img/image.jpg

Works well on localhost (with that filepath), although when I upload it to a web server, it doesn't seem to find the image no matter how hard I try to describe the path (the image is there, obviously, but I must be missing something very simple). Tried using THEME_DIR_IMG, HTML_PATH_THEME_IMG, HTML_PATH_ROOT, nothing works on a web server. I'm getting confused, though it must be something silly that I've missed.

Thanks in advance!
User avatar
Edi
Site Admin
Posts: 1318
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Contact:

Fri Dec 14, 2018 10:03 am

Works on my side.

Is Bludit installed in the root directory or in a subdirectory? Which permissions are set for the image?
Planet Bludit, Tipps, Snippets und nützliche Links. - Newsletter, Informationen zu Bludit (auf Deutsch).
User avatar
diego
Site Admin
Posts: 658
Joined: Sat May 16, 2015 2:53 pm
Contact:

Fri Dec 14, 2018 7:40 pm

I recommend using the following variables

Code: Select all

<!--IMAGE EXISTS-->
<?php if (file_exists(THEME_DIR.'img/image.jpg')): ?> 
<?php echo "Apples" ?>
<?php endif ?>

<!--IMAGE DOESN'T EXIST-->
<?php if (!file_exists(THEME_DIR.'img/image.jpg')): ?>
<?php echo "Oranges" ?>
<?php endif ?>
User avatar
battlethem
Jr. Bludit
Posts: 4
Joined: Sun Dec 09, 2018 7:52 pm

Sun Dec 23, 2018 1:05 pm

Alright this was clearly a mistake I made, it does actually work the way I described it in the first post, have no idea why it didn't work at that moment.
But I agree, using THEME_DIR. is much safer for that reason.
Thanks for the help anyway!
Post Reply