I have a method in a class I would like to use in order to show an image in a page that could be placed in any directory.
Basically, the class file would be at the root directory and the class would look like this:
class A{
function showImage(){
echo '<img src="img/file.jpg">';
}
}
Problems occur when I call the method in a page which is in a subdirectory.
For example, if I use showImage()
in 'subdir/mypage.php', then the image link is broken.
I have looked into $_SERVER['HTTP_HOST']
and dirname(__FILE__)
but I coud not find the right mix to get the right image path that could be used in order to show the image from any directory.
What function should I used in the class to get the file path dynamically that would depending on the directory where I am calling showImage()
?
Many thanks