/    Sign up×
Community /Pin to ProfileBookmark

php requires for template images

how do i ensure image paths are correct for php requires that are pulled in as you drill down? if i pull in the same require for the banner on every page, on every level, i lose the image path.

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@GenixdeaeAug 14.2005 — it all depends on where the include is being executed, NOT where the script is located. I was facing this same issue with my cms i'm building. What you have to keep in mind is that you could have that script in folder includes/images/banners/banner_top.php but it it's executed in index.php(in the root of the site) the image path is going to be images/banners/image.xxx where as if its in a folder called messaging/index.php it's going to now be ../images/banners/image.xxx....

Something you could try(as i'm not sure if this would even work, or be a good idea) is something like[code=php]$get_url = explode($_SERVER["DOCUMENT_ROOT"], "/");
$go_back = count($get_url); //count how many folders we are up
$img_link = ""; //define the variable first
if($go_back != 0 || $go_back != NULL) { //i'm not sure if these are necasary or if doing if(!empty($go_back)) { would work
for($i=0;$i<=$go_back;$i++) {
$img_link .= $img_link ."../";
$i++;
}
}
$img_link .= $img_link ."link/to/image/from/root.xxx";[/code]
Like i said i have no idea if that'll work or not, but it's worth a shot

Edit:::

I just noticed an issue...if you're site is part of a subdomain(eg: hostsite.com/~yousite) you'll always return 1 in the explode...to get around that you can just change the if statement to say[code=php]if($go_back == 1) {[/code] and that'll take care of that issue
Copy linkTweet thisAlerts:
@selyauthorAug 15.2005 — ok, i'll give this a shot. thank you
Copy linkTweet thisAlerts:
@bokehAug 15.2005 — Blimey! Does he really need all that code? Why cant he just do this:
[code=php]require($_SERVER["DOCUMENT_ROOT"].'/path/to/file/filename.ext');[/code]
it is even possible to go backwards from root like this:
[code=php]require($_SERVER["DOCUMENT_ROOT"].'/../../includes/filename.ext');[/code]

Truth is though, I am confused. He said image paths and those would need to be http paths. Sely can you clarify what you are trying to do?
Copy linkTweet thisAlerts:
@GenixdeaeAug 15.2005 — if i understood him right, he's tring to make a template with images and includes. The only catch is, if you have an included file in %root_dir%/includes/includes.php and in there you have your locations to the images(path/to/image.ext)...if you include that include into a differant directory(such as %root_dir%/applepie) those locations, in the includes file, now need to have a ../ added to the front of them which will now make it so that they wont display in %root_dir% because they have the ../

Now your way will work but that would mean doing that on every page(which defeats the purpose of a template)

i easly could have mis understood what he was looking for tho
Copy linkTweet thisAlerts:
@bokehAug 15.2005 — OK! In that case he needs an http path and I think an absolute path would be best.

Just a simple '/images/image.jpg' would work no matter what directory the script was in.

This references http root if called from an html page by a browser and document root if read by a script.
Copy linkTweet thisAlerts:
@chongAug 16.2005 — just define an absolute path?

define (PATH_IMG,'/images/');

then call by

require(PATH_IMG . 'image.jpg');
×

Success!

Help @sely spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...