/    Sign up×
Community /Pin to ProfileBookmark

Relative or Absolute Path…?

I’m not even sure how or what to ask about this, I have a feeling that either of the functons dirname() and/or basename() hold an answer to my question, but I’m not sure… At this point, I have a separate header.php file for each of the main directories of my website, but I want to be able to use just one, to keep everything even more unified and make skinning the website easy.

But I’m not sure how to get everything to work. Obviously, if I include the header.php that is in the main /public_html/ directory, in a file that is in /public_html/portfolio/, paths to links and images are going to be broken. How can I define the paths to the links and images in that main header.php so they will always point to the same place, no matter where the script calling on the file is?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@SheldonSep 28.2005 — use a full path in the included page

ie, rather index.php or contact.php use

http://www.domai/index.php and

http://www.domai/contact.php
Copy linkTweet thisAlerts:
@bokehSep 28.2005 — You are best off using an absolute path so that if for example you started using mod_rewrite the paths will not break. Here is an example of an image tag in header.php:
[code=php]<img src="http://<?php print $_SERVER['HTTP_HOST']; ?>/root/relative/path/file.ext" alt="">[/code]
Copy linkTweet thisAlerts:
@toplisekAug 06.2009 — What if there is usage of included files not for the path tags?

Is better to use relative path for includes?

Absolute paths in use makes inclusions unreliable across hosts, and using document root would force you to work from the root of you're site instead of relative to whatever script you have.
Copy linkTweet thisAlerts:
@thedigitalnomadauthorAug 06.2009 — Holy crap necropost! Do you realize the last post in this thread was made in 2005? I'm quite certain that the original issue was resolved long ago.

Why would you want to have inclusions across hosts, by the way? Wouldn't that be incredibly inefficient in the first place?

Actually, I'm not even sure what the hell you are talking about.
Copy linkTweet thisAlerts:
@MindzaiAug 06.2009 — I think toplisek means that hardcoding absolute paths hinders the portability of code. And it's pretty clear there is a language barrier here so it's understandable that there's a bit of a lack of clarity.
Copy linkTweet thisAlerts:
@thedigitalnomadauthorAug 06.2009 — The method that Bokeh posted four years ago would be the appropriate one, and does not hinder the portability of code.
Copy linkTweet thisAlerts:
@MindzaiAug 06.2009 — bokeh's example was for absolute URIs, toplisek is talking about filesystem paths.
×

Success!

Help @thedigitalnomad 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...