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

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: 3120
Joined: Sun Aug 09, 2015 5:01 pm
Location: Zurich
Has thanked: 54 times
Been thanked: 77 times
Contact:

Works on my side.

Is Bludit installed in the root directory or in a subdirectory? Which permissions are set for the image?
Clickwork - Websites mit Bludit | Planet Bludit - Tipps und Snippets
User avatar
diego
Site Admin
Posts: 773
Joined: Sat May 16, 2015 2:53 pm
Been thanked: 1 time
Contact:

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

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