Re: Seeking help with relative and absolute paths [message #180821 is a reply to message #180817] |
Wed, 20 March 2013 10:46 |
Jerry Stuckle
Messages: 2598 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 3/20/2013 12:45 AM, The Frog wrote:
> Hi Everyone,
>
> Just playing with some PHP and getting my head around some of the 'gotchas' that this language and its use contain. I am hoping that there is a simple answer to this issue I am facing...so here goes:
>
> I am designing a site (shock and horror) where I have the different elements of the site broken into different directories under the root of the site. On my development machine this is var/www/sitename as the site root. In the site root I have an index.php file that works quite happily.
>
> I have a directory under the site root called config, and another called scripts. In the config directory I have some basic stuff like constants that I define for the site (eg/ db.php for the database constants such as username, server, etc...). Referencing this with the following php from index.php works fine:
> <?PHP require_once 'config/db.php';
> require_once 'config/config.php';
> //do stuff here
> ?>
>
> Where I am having a problem is when I try to reference the files from a script that is in the scripts directory (or any other for that matter). I have tried to set a constant in the config/config.php file that points to the root of the site with the following:
> define(APP_ROOT, realpath( dirname( __FILE__ ) ).'/');
>
> This seems to be working for this test site, but I am still concerned that this wont work anywhere but the test site. I am also concerned that there seems to be no immediately obvious way to establish this value once for the site and simply be able to reference it without having to 'require_once' it in every script that might need it (which is most of them). I have tried to set things this way so that all scripts work with the APP_ROOT constant as the basis of referencing each other in a known way (ie/ absolute paths relative to root of the site).
>
> I feel like I am missing something glaringly obvious as this feels to me like a kludge or work-around. I am not used to coding in PHP so I thought I'd ask those who know more than I do. Can anyone clarify for me if I am on the right track or is there a simpler and more reliable (ie/ define once and its done) way of approaching this?
>
> Many thanks in advance
>
> The Frog
>
> Dev Machine:
> Ubuntu 10.10
> Apache 2.2.14
> PHP 5.3
>
As Aho, said, use the superglobal $_SERVER['DOCUMENT_ROOT']. I don't
like using the include config directive - it's too easy to forget about
it and use the wrong copy of a file of the same name.
Also, I recommend you keep non-web accessible files such as
configuration files out of the web directory structure, such as
$_SERVER['DOCUMENT_ROOT'] . '/../include/config.php'. This just ensures
the file can't be accessed should there be a screwup in the web server
config (which shouldn't happen - but does).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex(at)attglobal(dot)net
==================
|
|
|