This should be easy for PHP masters and i am really struggling with it.
My question: How do you specify the relative path for include or require functions
For example, if my directory hierarchy is like this:
[text]
root
|--dirA
|--- design
| ---- header.php
| ---- dirB
| ---- dirc
[/text]
How do is specify the relative paths for include and require functions to access:
a) header.php from root
b) header.php from dir b
c) header.php from dir c
I somewhere read that you need to specify some path in php.ini. I really dont want to do all that and really hope that PHP has simpler method for specifying paths.
Thanks a lot !
Specifying Relative path
Moderator: General Moderators
Re: Specifying Relative path
The trick is to not use relative paths. It looks like they are but they really aren't.
Using (c) as an example,
The first one is the best because you never have to think about traversing directories upwards - like how many /../s you need to include. It's also much more readable.
The include_path is an option but IMO it's a lot easier to do it in the code. Lots less guesswork.
Using (c) as an example,
Code: Select all
// always relative to the root directory
include $_SERVER["DOCUMENT_ROOT"] . "/dirA/design/header.php";
// relative to the directory containing the current file
include __DIR__ . "/../../dirA/design/header.php"; // PHP 5.3+
include dirname(__FILE__) . "/../../dirA/design/header.php";The include_path is an option but IMO it's a lot easier to do it in the code. Lots less guesswork.
Re: Specifying Relative path
thanks.. great insight..got me working with the include and require paths.
However i am now stuck in handiling the CSS file reference within the header.php
The php code
yields an output that specifies the file path on the hard disk C:\
<link rel="stylesheet" type="text/css" href= "C:\wamp\www\psytest\design\default.css">
where as it is requred to resolve to localhost://testsite/design/default.css or tho the http protocol on a live site.
How is this achieved ?
Thanks for your previous reply.
However i am now stuck in handiling the CSS file reference within the header.php
The php code
Code: Select all
<link rel="stylesheet" type="text/css" href= "<?php echo dirname(__FILE__) . '\default.css';?>"<link rel="stylesheet" type="text/css" href= "C:\wamp\www\psytest\design\default.css">
where as it is requred to resolve to localhost://testsite/design/default.css or tho the http protocol on a live site.
How is this achieved ?
Thanks for your previous reply.
Re: Specifying Relative path
Apples and oranges.
The best option is to use absolute paths all the time.
The best option is to use absolute paths all the time.
Code: Select all
<link rel="stylesheet" type="text/css" href="/default.css" />Re: Specifying Relative path
Thanks..