Re: Seeking help with relative and absolute paths [message #180824 is a reply to message #180821] |
Wed, 20 March 2013 18:10 |
J.O. Aho
Messages: 194 Registered: September 2010
Karma:
|
Senior Member |
|
|
On 20/03/13 11:46, Jerry Stuckle wrote:
> 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.
Yeah, the config has a lot of dangers, but it may fit some projects
better if they deploy the php.ini file for the site too (depending how
the web server is configured, you may have site specific configs instead
of one global), but that is for the OP to discover which way is best for
him, but I do agree with you with the simplicity of using the super global.
> 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).
Sadly quite many projects don't follow this rule, I guess they want to
make things to work out of the box for those who has a less good web
host. Keeping files which you don't people to have direct access too are
good to keep out of the document root too.
--
//Aho
|
|
|