Specifying Relative path

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Specifying Relative path

Postby Live24x7 » Tue May 22, 2012 11:53 pm

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:
Syntax: [ Download ] [ Hide ]
root
  |--dirA
        |--- design
                  | ---- header.php
  | ---- dirB
            | ---- dirc
 

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 !
Live24x7
Forum Contributor
 
Posts: 194
Joined: Sat Nov 19, 2011 10:32 am

Re: Specifying Relative path

Postby requinix » Wed May 23, 2012 1:40 am

The trick is to not use relative paths. It looks like they are but they really aren't.

Using (c) as an example,
Syntax: [ Download ] [ Hide ]
// 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 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.
The terror of immortality is eternal solitude. An endless reality tormented by the consciousness of one's sins.
User avatar
requinix
Spammer :|
 
Posts: 4795
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Specifying Relative path

Postby Live24x7 » Wed May 23, 2012 2:49 am

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

Syntax: [ Download ] [ Hide ]
<link rel="stylesheet" type="text/css" href= "<?php echo dirname(__FILE__) . '\default.css';?>"


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.
Live24x7
Forum Contributor
 
Posts: 194
Joined: Sat Nov 19, 2011 10:32 am

Re: Specifying Relative path

Postby requinix » Wed May 23, 2012 3:24 am

Apples and oranges.

The best option is to use absolute paths all the time.
Code: Select all
<link rel="stylesheet" type="text/css" href="/default.css" />
The terror of immortality is eternal solitude. An endless reality tormented by the consciousness of one's sins.
User avatar
requinix
Spammer :|
 
Posts: 4795
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Specifying Relative path

Postby Live24x7 » Wed May 23, 2012 6:09 am

Thanks..
Live24x7
Forum Contributor
 
Posts: 194
Joined: Sat Nov 19, 2011 10:32 am


Return to PHP - Theory and Design

Who is online

Users browsing this forum: No registered users and 2 guests