Question

When using php include function, I can easily use the same code on multiple pages, now the problem I ran into was, that my include file, outputs and image.

As the page where the include is called changes, so does the location of the files, that the include calls. Get it?

Example: I got a called "index.php". I also got a file called "load.php". and I got a file at last called "/folder/index2.php"

index.php code:<?php include("load.php"); ?>

index2.php code: <?php include("../load.php"); ?>

load.php code: <?php echo '<img src="image.png"/>'; ?>

When loading index.php, the image is shown perfectly as it ought to. When loading index2.php, which is located in another folder, but loads the "load.php" file successfully, the image is saying it cannot be found, which makes sense, since index2.php is calling the image, as if it was in the same folder, which it isn't.

So, is there any way to do this? I'm trying to avoid using a full url, but if it's the only way I'll use that. I'm just looking for a better option.

Was it helpful?

Solution

First off, try specifying your include files using the document root: $_SERVER['DOCUMENT_ROOT']... this will always direct to the public root folder so it will be the same no matter where in the site you are - this is particularly useful when you start including files which themselves include/require other files... in that scenario an included script which itself included sub-scripts relatively (../load.php|subfolder/load.php, etc) would never work from two different levels in the folder hierarchy.

<?php include($_SERVER['DOCUMENT_ROOT']."/load.php"); ?>

Then for resources that are being called in html, simple make them all relative to the public root as well:

<?php echo '<img src="/image.png"/>'; ?>

Notice the / in front of the image src? That will always call the image no matter what folder the currently displayed page is in.

Alternately, you could just specify resources absolutely (though this isn't ideal as if the site domain changes it will break the resource links):

<?php echo '<img src="http://www.yoursite.com/image.png"/>'; ?>

Either way, no matter what script you're calling the include from it will always grab it from the correct location (no need to add ../ etc in your include calls) and images/scripts/css/etc will all work no matter what page they're pulled into

OTHER TIPS

$_SERVER['DOCUMENT_ROOT'] indicates the current public root in include, as mentioned.

The image source can be started with slash: <img src="/image.png"> to tell the browser to look at the domain root. The mentioned src would tell the browser to check www.example.com/image.png, if placed in any document under www.example.com, or any of its sub-directories.

Edit: Ben was faster at editing

i suggest make a config.php

use a variable for full path E.g. $imgPath = 'www.host.com/path/images/';

include it at start of every page,

in load.php use <?php echo '<img src="'.$imgPath.'image.png"/>'; ?>

now when you include the load.php, you will have no errors :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top