Reputation: 15
So here is the structure of my basic practice website
/opt
/lampp
/htdocs
/web
/img
logo.png
/tests
HomeView_test.php
/view
HomeView.class.php
index.php
My initial problem was that I am using auto loading and running HomeView.class.php from index.php and HomeView.class.php from HomeView_test.php would not load my logo image on both if I only hard coded the path from one. So for example if I hard coded the path to work when called from index.php it would not also work when called from HomeView_test.php
Here is what is in HomeView.class.php
<?php
class HomeView {
public static function show() {
$pathDir = dirname(__FILE__);
$fileName = $pathDir . DIRECTORY_SEPARATOR . "../img/logo.png";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Website</title>
</head>
<body>
<header>
<img src="<?php echo $fileName;?>" alt="Logo" width="150" height="100"/>
<nav>
<ul>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact Us</a>
</li>
</ul>
</nav>
</header>
<footer>
<p>
© 2015 Peeps, Inc. All Rights Reserved.
</p>
</footer>
</body>
</html>
<?php
}
}
?>
I thought that the the php echo of the path inside the src would work but it is still not loading the logo image.
Hard coding in img/logo.png only works when in index.php it calls HomeView.class.php but when I call it from HomeView_test.php it needs ../img/logo.png to show the logo
Does anyone have any idea as to what I am doing wrong? Maybe the img src and php syntax is off since I've never tried using php in conjunction with img src before.
Upvotes: 0
Views: 1410
Reputation: 1524
In this case, when you want to show an image from a web page, regardless of what backend language you are using, you need to provide the public server path to the media.
So when you have this statement:
<img src="<?php echo $fileName;?>" alt="Logo" width="150" height="100"/>
The variable $filename
ought to look something like /img/logo.png
when outputted in HTML.
Your goal is to provide the public web location of the url, not the server location.
Upvotes: 0