Buyocat Posted August 1, 2006 Share Posted August 1, 2006 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.phpHowever 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.phpThis 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.EDITI 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. Quote Link to comment https://forums.phpfreaks.com/topic/16222-relative-paths-with-php/ Share on other sites More sharing options...
wildteen88 Posted August 1, 2006 Share Posted August 1, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/16222-relative-paths-with-php/#findComment-67114 Share on other sites More sharing options...
Buyocat Posted August 1, 2006 Author Share Posted August 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16222-relative-paths-with-php/#findComment-67120 Share on other sites More sharing options...
wildteen88 Posted August 1, 2006 Share Posted August 1, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/16222-relative-paths-with-php/#findComment-67129 Share on other sites More sharing options...
gerkintrigg Posted August 1, 2006 Share Posted August 1, 2006 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) Quote Link to comment https://forums.phpfreaks.com/topic/16222-relative-paths-with-php/#findComment-67283 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.