php file path require syntax for importing a page - php

I have a directory called images and about and 3 php file on home directory
and 3 files and have different content
footer.php
This is footer <img src="images/logo.png">
header.php
<h1>Welcome to MyWebsite</h1>
index.php
<?php
require('header.php'); ?>
Enter your name : and some forms and javascript code
<?php
require('footer.php'); ?>
Now i have file in directory about as about.php
and it has some contents and below code is
about.php
<?php require('../header.php'); ?>
This is About page<br>
<?php require('../footer.php'); ?>
And when i open the page about.php, the footer is working fine but the image is not showing up, and image directory has image as logo.png
Even i used realpath to work out with relative paths, but could not display.
Even i tried this one too in footer.php
<?php
define('__ROOT__', images(__FILE__));
?>
This is footer <img src="<?php echo require_once(__ROOT__.'/logo.png); ?>">
I tried out the possibilites of relatives paths, do we have any other thing.

If I'm understanding your question correctly, the easiest solution is probably just to use an absolute path for your image tag in footer.php:
So if images is in your docroot, it would look like this:
This is footer <img src="/images/logo.png">

Including images in the php file makes absolutely no sense.
You have to understand the difference between PHP code and HTML code.
PHP code being executed on the server and have result of HTML code sent to the browser.
Where browser reads that HTML and do additional requests to the server to get images.
Thus, server filesystem root has absolutely nothing to do with browser.
As for the problem - Trott's answer is perfect.

Well if you are using include() to get certain section of the Page with PHP, there will definitely be a problem with links.
I believe the best practice is to call images using CSS. Just get a div with a class or id and place the relative link in the CSS.
It worked for me.

I got the answer for this questions
copy the below code in header.php and it will work every where
<?php
define('ROOT_DIR', dirname(__FILE__));
define('ROOT_URL', substr($_SERVER['PHP_SELF'], 0, - (strlen($_SERVER['SCRIPT_FILENAME']) - strlen(ROOT_DIR))));
?>
And where ever there is a image you src="" just put the below code only with imagename changed, and whatever you want.
<img src="<?php echo ROOT_URL .'/images/logo.jpg'; ?>">

Related

Include html file with image using PHP

I am a newbie in website development.
here is my problem :
I have 2 HTML files. they are 'index.php' and 'header.php' . I try to include 'header.php' in to the 'index.php' using this code :
'index.php'
<body>
<?php
include("header/header.php");
?>
</body>
'header.php' contain this code :
<h1>Its header</h1>
<img src="img/006-tumblr.png" width="200" height="200">
its the folder hirearchy :
-index.php
--header
--img
-006-tumblr.png
-header.php
When I open 'index.php' , 'header.php' is included but the image is not displaying.
So how can I include 'header.php' with the image?
A good way of proceeding would be to have a folder "img" in your root public HTML path containing all images, eventually with subfolders to separate them. The reason for that is that your main controller is launching from root. In that way, wherever you call your image file, you just have to go to img/ in order to find it. The same logic applies to all media. So, if you intend to have video for example, you could have a main folder media with a img subfolder and a vid subfolder. This type of logic has to be defined at start of project so that you don't have to refactor in the middle of it.
The logic of including a file into another is different from linking a CSS file. When you include a PHP file, the entire code is added to the source file and then the server compiles the codes. So the image files and other resources should be addressed relative to the source document (not the included one). This logic is different in a CSS file and the resource files e.g. a background image is complied relative to the CSS file (Because a CSS may be used in different files hirearchy). So this will work:
<h1>Its header</h1>
<img src="header/img/006-tumblr.png" width="200" height="200"
Footnote: If you want to use header in different files with different hirearchy the solution for the question above is to set BASEURL for your document and setting the resource and anchores relative to the baseurl.
Giving location from root directory makes image accessible from any location.
<?php define('WEBSITE_BASE', $_SERVER["SERVER_NAME"]); ?>
<img src="<?=WEBSITE_BASE?>/header/img/006-tumblr.png" width="200" height="200">
Best linking policy you should always follow to link static assets to your page so that it links wherever the item is used.

Issues with relative paths in PHP includes

Before going on, I'd just like to say that I've tried pretty much everything, and I read a lot of questions here at Stackoverflow, but none of the solutions worked.
So here is the deal, I have a file ("empresaConf.php") that contains this code:
<?php
include 'navigator2.php';
?>
<div class="contTudo">
<div id="bgCorpo">
<div class="empresaConf">
<?php
include 'sqEsqInc.php'
?>
</div>
</div>
<?php
include '../rodape.php';
?>
</div>
</body>
</html>
As you can see, it is including "rodape.php". The problem is that in "rodape.php", I have an image that is in another folder, so when I include "rodape.php" in "empresaConf.php", the image path should change, since PHP include consider the file path to be the one that you're including other files, making rodape.php's image broken when I access "empresaConf". I want something to fix that so I have the image no matter what file I'm in. Here is the "rodape.php" code:
<div class="empresa">
/* below in the img src tag you can see what I'm talking about */
<div class="redeSocial">
<a href="http://www.facebook.com.br">
<img src="imgs/icons/iconFace42.png" class="iconeFace">
</a>
</div>
</div> <!-- Fim div .empresa -->
The folder structure is: "rodape.php" is in the root directory and "empresaConf.php" is inside a folder named "Empresa" (I would post a img but I can't).
Thanks in advance.
EDIT:
Sorry, I forgot a crucial detail, I'm also including "rodape.php" in other files such as "index.php". So if I change the img src to correspond "empresaConf.php", then in the "index.php" the image will be broken, since it's in the root directory, same as "rodape.php".
If it's an image accessible from the public side of things, the image can probably be accessed by the directory root. So if your index.php is in /index.php where / is the web document root and your image is in /imgs/icons/iconFace42.png you can access the image by
<img src="/imgs/icons/iconFace42.png" class="iconeFace">
Note the leading slash in the src attribute.
You have some options.
If rodape.php will only be accesed from that folder level, simply change the src to ../imgs/icons/iconFace42.png, if you will be changing folder deep then use an absolute path to the image. Another way would be add php code to set the src but that would be wast of resources.
When you include ../rodape.php, think of it like just putting the code from that file directly in the position where it was included. The path string will not change, as it has not been executed until it has been included.
In short, that is to say, things will be relative to the file that's being executed, not the file(s) that's being included.
<img src="../imgs/icons/iconFace42.png" class="iconeFace"
Using that image code should resolve your issues.

Load an image from included php not in root directory of server

I'm stuck in a simple thing as loading an image in php.
All included php files are in a directory called includes.
Index.php is in root folder.
When I call img src from the php file located in the root folder, I can see images.
But when I include a section that is supposed to load an image like that:
<?php include("includes/section.php"); ?>
calling this:
<div class = "right">
<h1> news </h1>
<hr>
echo '<img src="../img/do.png" width="200" alt="Rifrazione news/>';
<p class = "rightp">Blabla </p><br
</div><!--end of right-->
I get nothing
for images I prefare use global variable.
Try do something like this:
<?
$baseUrl = "http://localhost"; // CHANGE IT TO YOUR URL
?>
<div class = "right">
<h1> news </h1>
<hr>
<?
echo '<img src="'.$baseUrl.'/img/do.png" width="200" alt="Rifrazione news" />';
?>
<p class = "rightp">Blabla </p><br />
</div><!--end of right-->
The file you're currently in is your current working directory. So find the location of your image from the directory your index.php is in, that means probably removing the ../. If you have any doubts echo getcwd();.
NEVER do what Adam suggested. You should always use file system paths, never url paths to local files. EDIT Reasoning: Hotlinking is part of it as he may not be aware of why something isn't working as it has worked in other locations but mostly due to routing/.htaccess/url rewriting. If he were to move on later to an MVC or similar framework putting localhost/controller/action will not return his image, but he may not be sure why as this is a bad habit he has already picked up. The other reason was for ease-of-use, if he puts a path like ./image/here.jpg opposed to localhost*/here.jpg he may have to go through and rewrite every link if he were to migrate later to an external server.
Example to help visualize better:
If you have the following directory:
my_site/
js/
css/
images/
image.jpg
index.php
bob/
bob.php
contactBob.php
If you were viewing index.php, your link to images would be /images/image.jpg.
If you were viewing contactBob.php your link to images would be ../images/image.jpg.
In HTML5, you can declare the <base> as a DOM element.
After searching and testing...it was none of that.
The only thing missing was the php tag at the beginning of the bracket.
You don't need variables at all.
This syntax is working as intended.
<?php
echo '<img src="img/do.png" width="120" alt="Rifrazione news"/>';
?>
Thanks for jumping in, anyway.

PHP Includes function, not displaying images

This is all that is contained on my home.php page
<?php
include ('../bgs/bg-verbier.html');
include('../menus/menu-verbier.html');
?>
both of the requested pages lie in the parent directory. The /menus/menu-verbier.html has posed no problem from the include function, however the '../bgs/bg-verbier.html is. The file as been found however the images aren't displaying.
The path of the images is working on a .php page that lies in the same parent directory,
Here's the ../bgs/bg-verbier.html' file:
<div id="bg">
<b><i><i><img src="images/verbier/0.jpg"></i></i></b>
<!--b:holder, i#1: large canvas, i#2: center verticaly -->
<b><i><i><img src="images/verbier/1.jpg"></i></i></b>
<b><i><i><img src="images/verbier/2.jpg"></i></i></b>
<b><i><i><img src="images/verbier/3.jpg"></i></i></b>
</div>
<script type="text/javascript" src="js/scaler.js"></script>
<script type="text/javascript" src="js/example.js"></script>
Your images folder must be in the same directory as the php file that is calling it. Otherwise, you must specify the relative path to the images folder.
When you include a file, it's like if you were to copy & paste that file's contents into the PHP file.
So, it's like the PHP file is asking for images/verbier/0.jpg, which means it's looking in the same folder as the PHP file.
I suggest you edit the HTML file so it uses absolute paths to the images, like:
<img src="/menus/images/verbier/0.jpg">

PHP include doesn't display pictures

I have a small problem. I'm not good in php, so this may be something really simple. Basically, I have couple of websites, which I want to implement a specific picture on. I want it to be controlled (along with a hyperlink) from one script. So what I've done is this:
<?php include 'http://content.captive-portal.com/pd/sponsored/sponsored.php' ?>
I have placed this code on couple of websites but it doesn't display a picture. Why? Can it be done this way?
The page I placed this script on is this one:
amc link - the image should appear just underneath a logo. The gif image from the script above. Can anyone please help me out with this? What have I done wrong? can't it be done? THank you for your help.
The code is here:
<div id="wrapper">
<img id="logo" src="images/logotop_m.jpg"/>
<?php include 'http://content.captive-portal.com/pd/sponsored/sponsored.php' ?>
</div> <!--eo wrapper -->
...
Is the relative path causing the issue? Does the sponsored.php file make reference to the image using a complete path? (e.g.... http://content.captive-portal.com/images/.... or just images/...)
The reason for that is that some images are being used via relative links. Here's the logo:
<img id="logo" src="images/logotop_m.jpg"/>
As it isn't an absolute path with a domain, it'll resolve as the following:
http://yourdomain.com/images/logotop_m.jpg
Instead of what you're expecting:
http://amc.captive-portal.com/images/logotop_m.jpg
Edit: Oops
I didn't notice the redirect as I opened it in a new tab - my bad. The php file itself contains this:
<p><img src="http://content.captive-portal.com/pd/sponsored/sponsored.gif" width="480" height="80"></p>
As it's an absolute path, it *should* be alright.

Categories