PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Fri Nov 21, 2014 9:26 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Specifying Relative path
PostPosted: Tue May 22, 2012 11:53 pm 
Offline
Forum Contributor

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


Top
 Profile  
 
PostPosted: Wed May 23, 2012 1:40 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5644
Location: WA, USA
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.


Top
 Profile  
 
PostPosted: Wed May 23, 2012 2:49 am 
Offline
Forum Contributor

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


Top
 Profile  
 
PostPosted: Wed May 23, 2012 3:24 am 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 5644
Location: WA, USA
Apples and oranges.

The best option is to use absolute paths all the time.
Code:
<link rel="stylesheet" type="text/css" href="/default.css" />


Top
 Profile  
 
PostPosted: Wed May 23, 2012 6:09 am 
Offline
Forum Contributor

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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: Exabot [Bot] and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group