Can't display image

chriso0258 31 Mar, 2014
Hello,

I'm trying to figure out why I can display an image when the page is my home page, but if I link to it from a menu item I can't display the image.

The first image show what happens when the page is my home page.
[attachment=0]image_prob1.jpg[/attachment]

This image shows what happens when the page is not my home page but is accessed from the menu.
[attachment=1]image_prob2.jpg[/attachment]

Here is the php code I've used:

$picdir='images/staff/';
$picname=$picdir.$binumber.".jpg";

// Check to see if image is in directory
   if (file_exists($picname)) {
      echo "<p align=center><img src=$picname><br><br>";
   } else {
      echo "<p align=center><br><font size=+2 color=#000000;>*** No picture available. Contact IFTO Office to update.***</font><br><br>";
   }


As you can see from Firebug, the path is the same. The page is public set to public.

My webserver is a Ubuntu 13.04. Permissions for the image directory is 777.

Any ideas?

Thanks for your assistance.
GreyHead 01 Apr, 2014
Hi chriso0258,

You can't safely use relative URLs in Joomla! as you are not certain where the code will be run from. Please try adding a leading / to the image path to make it absolute:
$picdir = '/images/staff/';

Bob
chriso0258 01 Apr, 2014
Thanks for your reply Greyhead,

Here is the updated code. I tried your suggestion and added the '/' as you can see in the code below. I also tried using the full path as you can see in the commented out part. Below are images of the results of each.


$picdir='/images/staff/';
//$picdir='/var/www/images/staff/';
$picname=$picdir.$binumber.".jpg";

// Check to see if image is in directory
   if (file_exists($picname)) {
      echo "<p align=center><img src=$picname><br><br>";
   } else {
      echo "<p align=center><br><font size=+2 color=#000000;>*** No picture available. Contact IFTO Office to update.***</font><br><br>";
   }


Here is the picture with the correction you suggested:
[attachment=0]image_prob4.jpg[/attachment]

As you can see, the script does not see the image so it gives me the message the image doesn't exist.

Here is the picture when using the full path:
[attachment=1]image_prob3.jpg[/attachment]

Here, it appears to have found an image but is not displaying it.

I'm afraid I'm at a loss. Is there something wrong with the php code or the way Joomla handles php? Or could it be a syntax issue concerning Ubuntu (13.04)?

Thanks for any insight.
GreyHead 02 Apr, 2014
Answer
Hi chriso0258,

Hmmm . . I over-simplified a little bit.

To check if the file exists you need to specify the full path /var/www/images/staff/image_name.jpg

To display it you need to use the URL http://my_domain.com/images/staff/image_name.jpg or just /images/staff/image_name.jpg

Debug carefully and it should work OK.

Bob

<?php
$pic_dir='images/staff/';
$pic_path = JPATH_SITE.'/'.$pic_dir;
$pic_url = JURI::root().$pic_dir;
$pic_name = $binumber.".jpg";

// Check to see if image is in directory
if ( file_exists($pic_path.$pic_name ) ) {
      echo "<p align='center'><img src='{$pic_url}{$pic_name}' /><br /><br />";
} else {
      echo "<p align='center'><br><font size=+2 color=#000000;>*** No picture available. Contact IFTO Office to update.***</font><br /><br />";
}
?>
chriso0258 03 Apr, 2014
Thanks Bob! That worked. BTW, I went to your buy me a coffee page and completed your form and it took me back to your buy me a coffee page. Was it suppose to take me to paypal?
This topic is locked and no more replies can be posted.