PHP Developers Network
http://forums.devnetwork.net/

Specifying Relative path
http://forums.devnetwork.net/viewtopic.php?f=19&t=135751
Page 1 of 1

Author:  Live24x7 [ Tue May 22, 2012 11:53 pm ]
Post subject:  Specifying Relative path

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 !

Author:  requinix [ Wed May 23, 2012 1:40 am ]
Post subject:  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,
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.

Author:  Live24x7 [ Wed May 23, 2012 2:49 am ]
Post subject:  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

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.

Author:  requinix [ Wed May 23, 2012 3:24 am ]
Post subject:  Re: Specifying Relative path


Author:  Live24x7 [ Wed May 23, 2012 6:09 am ]
Post subject:  Re: Specifying Relative path

Thanks..

Page 1 of 1 All times are UTC - 5 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/