Jump to content

Archived

This topic is now archived and is closed to further replies.

Buyocat

Relative paths with PHP

Recommended Posts

Hi, I'd really like to include dependant files by using the relative path notation for the filesystem.  For example I might want to grab a file that is up one directory then down one...
../somenewdir/somefile.php

However whenever I try to use this notation I get errors saying the file can't be found.  I suspect the problem may arise from the fact that I'm working from the url:
http://locahost/afile.php

This file is including the a file which is in turn including another file.  The middle file fails to find the third one with the notation outlined above.  Is this a result of my url pointing to one directory and the relative paths playing off the url?  Is there a way around this?  Mostly this is important because these includes are supposed to be done before a configuration file is written -- I'd like to keep them independant of a configuration and have them just rely on file structure.

Thanks for any help, Buyo.

EDIT
I just though I'd add that the middle file requires 2 files.  The first one is of the form:
[code]
require_once 'somefile.php';
[/code]
This require works just fine and it grabs a file in the same directory as the middle file, but the second...
[code]
require_once '../someotherfile.php';
[/code]
fails to load the file.

Share this post


Link to post
Share on other sites
That is becuase the secound require_once is using a ralative path to the first file and not relative to the secound file. A way around this to pre-pend $_SERVER['DOCUMENT_ROOT'] to the path like so:
[code]require_once $_SERVER['DOCUMENT_ROOT'] . '/somefile.php';[/code]

Share this post


Link to post
Share on other sites
Thanks Wildteen, but is there no other way?  As it stands that would be about the same as adding the configuration information, as I would need to tailor the results from $_SESSION['DOCUMENT_ROOT'] to fit my file path.  Maybe it isn't possible to have a file just move around the filesystem relative to its place in it.

Share this post


Link to post
Share on other sites
You can do relative filepaths to a file you want to include. But if the file you are including is including other files. Then PHp wont use the relative path for the included file but the file that is including the file. As with require or include PHP basically gets the contents of the file and pastes where it is being including to.

Share this post


Link to post
Share on other sites
For what it's worth I do it this way:
// root is defined per page and expresses the path to the root directory from that page:
[code]$root='../';[/code]

Then I call href tags and image tags like this:
[code]<a href="<?php echo $root;?>/whatever_folder/whatever_file.php">[/code]
or
[code]<img src="<?php echo $root;?>my_image_file.jpg">[/code]
This way you can refer to root in other include files and includes that use include files that use other include files (ad infinatum) all use the same $root variable as defined once in the page that calls them.

Ah, the simplest answers are always the greatest!! (now answer my posts!! ) <- Request, not a command!

Hope that helps. :o)

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.