Jump to content

Relative paths with PHP


Buyocat

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.
Link to comment
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]
Link to comment
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.
Link to comment
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.
Link to comment
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)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.