Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
Govinda wrote: I want something that will work for calling an include from any file that lives n levels deep. That's where you have to define a variable (or constant) that tells the system where the web root is located, and then use that to determine where you are in relation to that. For example: for($i=0;$i<(substr_count($me,'/') - substr_count($root,'/'));$i++) { $me = dirname($me); } return $me; // Returns the resulting path. } return false; // If we were unable to get the path. } /* Then use it as follows, presuming this file is named /home/user/public_html/web/home.php */ if(($path = relate_path(__FILE__)) !== false) { include($path.'/include/config.php'); } else { // Handle the error for the incorrect inclusion attempt. } ?> Voila! Also, what is the difference between a path that starts with "/", versus the same path but that does not have that leading "/", or that same path but prefixed with "./"? I.e., this: /somepath/includes/file.php is a true (absolute) path. versus this: somepath/includes/file.php is a relative path from wherever the file is called. versus this: ./somepath/includes/file.php is a relative path from the CWD/PWD (Current Working Directory/Present Working Directory). P.S. - The function is untested, just rattled off from my brain while I cook dinner, so if it doesn't work, at least you should get the gist of where I'm going but try it anyway. ;-P Dan I love to see smart hacks in action! ..and I believe I get what you are doing. I am just amazed that there is not a SIMPLE (one-liner) reliable way of just saying "document root" without a complex function like that. $documentRoot = '/srv/website/www' Unless you change your site configuration option, that is both concise and easy to understand when you (or someone else) reads the code 5 months from now. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
I'm not sure how this could be made simpler. $site_root = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR; Call that in any file in the root of *your* web directories, and you have what is essentially the "document root" for *your* site. Presumably, you know the *relative* directories of all your files. Simply append those relative directories + filenames to the $site_root variable, and you're done. Paul Paul, you are right. what you say above is very simple. I guess I just did a poor job of communicating what I was after. What I wanted was: ($_SERVER['DOCUMENT_ROOT'] ..but an equivelent that would work on any server, even if ($_SERVER['DOCUMENT_ROOT'] was turned off. SO that I could build a universal global include path that would not break in any page, even if I later moved the page up or down a directory.. never requiring editing, despite the page's URL changes. -G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Mon, Jul 06, 2009 at 07:15:03PM -0600, Govinda wrote: > > Dan I love to see smart hacks in action! ..and I believe I get what > you are doing. > I am just amazed that there is not a SIMPLE (one-liner) reliable way > of just saying "document root" without a complex function like that. > I mean what we need here is a global include path for the shared- > hosted user .. an include_path that can be set for the virtual server > user and it 'sticks' (does not have to be set on every page load). > I think I'll let this all go for now... I have a lot of basics to > cover before I trip out too much in any one area. I'm going to need > the time and brain juice for some 'real' issues later ;-) I'm not sure how this could be made simpler. $site_root = realpath(dirname(__FILE__)) . DIRECTORY_SEPARATOR; Call that in any file in the root of *your* web directories, and you have what is essentially the "document root" for *your* site. Presumably, you know the *relative* directories of all your files. Simply append those relative directories + filenames to the $site_root variable, and you're done. Paul -- Paul M. Foster -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
I want something that will work for calling an include from any file that lives n levels deep. That's where you have to define a variable (or constant) that tells the system where the web root is located, and then use that to determine where you are in relation to that. For example: for($i=0;$i<(substr_count($me,'/') - substr_count($root,'/')); $i++) { $me = dirname($me); } return $me; // Returns the resulting path. } return false; // If we were unable to get the path. } /* Then use it as follows, presuming this file is named /home/user/public_html/web/home.php */ if(($path = relate_path(__FILE__)) !== false) { include($path.'/include/config.php'); } else { // Handle the error for the incorrect inclusion attempt. } ?> Voila! Also, what is the difference between a path that starts with "/", versus the same path but that does not have that leading "/", or that same path but prefixed with "./"? I.e., this: /somepath/includes/file.php is a true (absolute) path. versus this: somepath/includes/file.php is a relative path from wherever the file is called. versus this: ./somepath/includes/file.php is a relative path from the CWD/PWD (Current Working Directory/Present Working Directory). P.S. - The function is untested, just rattled off from my brain while I cook dinner, so if it doesn't work, at least you should get the gist of where I'm going but try it anyway. ;-P Dan I love to see smart hacks in action! ..and I believe I get what you are doing. I am just amazed that there is not a SIMPLE (one-liner) reliable way of just saying "document root" without a complex function like that. I mean what we need here is a global include path for the shared- hosted user .. an include_path that can be set for the virtual server user and it 'sticks' (does not have to be set on every page load). I think I'll let this all go for now... I have a lot of basics to cover before I trip out too much in any one area. I'm going to need the time and brain juice for some 'real' issues later ;-) -G -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Mon, Jul 6, 2009 at 18:16, Govinda wrote: > > this is great, but then I still do not have a solution that will work for > any level deep of dir/ . > I.e. this- > dirname(dirname(__FILE__)) > gives the correct first part of the path to document root like > $_SERVER['DOCUMENT_ROOT'] does > only when called from a file that is 2 levels deep. > I want something that will work for calling an include from any file that > lives n levels deep. That's where you have to define a variable (or constant) that tells the system where the web root is located, and then use that to determine where you are in relation to that. For example: Voila! > Also, what is the difference between a path that starts with "/", versus the > same path but that does not have that leading "/", or that same path but > prefixed with "./"? > I.e., this: > /somepath/includes/file.php is a true (absolute) path. > versus this: > somepath/includes/file.php is a relative path from wherever the file is called. > versus this: > ./somepath/includes/file.php is a relative path from the CWD/PWD (Current Working Directory/Present Working Directory). P.S. - The function is untested, just rattled off from my brain while I cook dinner, so if it doesn't work, at least you should get the gist of where I'm going but try it anyway. ;-P -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Check out our great hosting and dedicated server deals at http://twitter.com/pilotpig -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Mon, 6 Jul 2009 16:16:55 -0600 Govinda wrote: > I do not really understand why > $_SERVER['DOCUMENT_ROOT'] > should return the right data at one time and not at another. (?) In general it will always provide the right data, but as the manual says: "The entries in this array ($_SERVER) are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some." The problem could arise if the script is transfered unto a web server where the $_SERVER array (or parts of it - in this case the document_root part) has been disabled. Take into consideration where the script/program has to run and whether it is likely to be moved around to different web servers with different setups. > Also, what is the difference between a path that starts with "/", > versus the same path but that does not have that leading "/", or > that same path but prefixed with "./"? > > I.e., this: > /somepath/includes/file.php This depends on whether the web server is running in a chroot. If the web server for example has access to all files on the machine and isn't running in any kind of chroot or limited setup, then "/somepath" is located in the very root of the directory on that particular hard drive (or partition) and "/somepath" is NOT a sub directory of another directory. So you would see something like this (if you are not using Windows): /var/www/mywebsite.com/ /somepath/includes/file.php /usr/sbin/ /home/foo/ > versus this: > somepath/includes/file.php This depends on where your script is running from. If your script is running in: /var/www/mywebsite.com/myscript.php Then the above would become: /var/www/mywebsite.com/somepath/includes/file.php If your script is running in: /var/www/mywebsite.com/subdirectory/myscript.php Then the above would become: /var/www/mywebsite.com/subdirectory/somepath/includes/file.php > versus this: > ./somepath/includes/file.php Its the same as "somepath/includes/file.php" a "./" means "current working directory". I hope I make sense. If you haven't already take a look at: http://php.net/manual/en/function.include.php > ("../" I know) > > -G --- Mange venlige hilsner/Best regards Kim Naim Lesmer Programmer/Unix systemadministrator Web: www.bitflop.com E-mail : k...@bitflop.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Jul 6, 2009, at 9:24 AM, Daniel Brown wrote: On Mon, Jul 6, 2009 at 11:04, Govinda wrote: Kim, this is exactly what I was looking for. I had been over $_SERVER in the docs.. but somehow missed that basic obvious param. Thanks! And now I'll throw a monkey wrench into the gears and tell you that, yes, it works, but don't always rely on it. The path translated to/from the web server may not be the real path. And while you could do your translation with realpath(), instead you should look into: file.php'); ?> The example above uses the always-on reserved constant __FILE__ for the file in which it's written, regardless of how it's accessed (as an include, etc.). So consider the following: /home/user/public_html/index.php /home/user/public_html/web/home.php /home/user/public_html/templates/body.tpl.php /home/user/public_html/include/file.php In the example above, imagine that you access 'index.php', which includes 'home.php', which - in turn - includes 'body.tpl.php' and 'file.php'. Using $_SERVER data will use the information given to it by the webserver at the point of the call: in this case, from /home/user/public_html/index.php. As such, the include path will be relative to that. So if you're including a file in ./web/home.php, you will need to hard-code the adjustment to account for the difference. Conversely, using the code example from above (and building upon it), we know that __FILE__ remains static regardless of the point of the call. Thus, it's a better and more reliable method, and is usable even if $_SERVER data is not available to the script. With this in mind, it should be quite simple to understand how the following should work: but to each his/her own! ;-P Michael and Dan this is great, but then I still do not have a solution that will work for any level deep of dir/ . I.e. this- dirname(dirname(__FILE__)) gives the correct first part of the path to document root like $_SERVER['DOCUMENT_ROOT'] does only when called from a file that is 2 levels deep. I want something that will work for calling an include from any file that lives n levels deep. Isn't there anything reliable that effectively says, "from document root"?? I do not really understand why $_SERVER['DOCUMENT_ROOT'] should return the right data at one time and not at another. (?) -- Also, what is the difference between a path that starts with "/", versus the same path but that does not have that leading "/", or that same path but prefixed with "./"? I.e., this: /somepath/includes/file.php versus this: somepath/includes/file.php versus this: ./somepath/includes/file.php ("../" I know) -G
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Mon, Jul 6, 2009 at 8:24 AM, Daniel Brown wrote: > Conversely, using the code example from above (and building upon > it), we know that __FILE__ remains static regardless of the point of > the call. Thus, it's a better and more reliable method, and is usable > even if $_SERVER data is not available to the script. +1 - i use dirname(__FILE__) everywhere. Rasmus said "you can just use ./includes/foo.php, why have an extra function call" (the dirname) but i tried that on one of my setups and what is odd is it couldn't find the files from the forced relative paths which should work just fine. there could have been other weird voodoo going on too, but i know for a fact dirname(__FILE__) has been reliable and the best part is it does not require $_SERVER. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Mon, Jul 6, 2009 at 11:04, Govinda wrote: > > Kim, this is exactly what I was looking for. I had been over $_SERVER in > the docs.. but somehow missed that basic obvious param. Thanks! And now I'll throw a monkey wrench into the gears and tell you that, yes, it works, but don't always rely on it. The path translated to/from the web server may not be the real path. And while you could do your translation with realpath(), instead you should look into: The example above uses the always-on reserved constant __FILE__ for the file in which it's written, regardless of how it's accessed (as an include, etc.). So consider the following: /home/user/public_html/index.php /home/user/public_html/web/home.php /home/user/public_html/templates/body.tpl.php /home/user/public_html/include/file.php In the example above, imagine that you access 'index.php', which includes 'home.php', which - in turn - includes 'body.tpl.php' and 'file.php'. Using $_SERVER data will use the information given to it by the webserver at the point of the call: in this case, from /home/user/public_html/index.php. As such, the include path will be relative to that. So if you're including a file in ./web/home.php, you will need to hard-code the adjustment to account for the difference. Conversely, using the code example from above (and building upon it), we know that __FILE__ remains static regardless of the point of the call. Thus, it's a better and more reliable method, and is usable even if $_SERVER data is not available to the script. With this in mind, it should be quite simple to understand how the following should work: but to each his/her own! ;-P -- daniel.br...@parasane.net || danbr...@php.net http://www.parasane.net/ || http://www.pilotpig.net/ Check out our great hosting and dedicated server deals at http://twitter.com/pilotpig -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Jul 5, 2009, at 4:42 PM, Kim N. Lesmer wrote: Like Michael said there is more than one way to deal with this. I personally prefer to use this: require_once ($_SERVER['DOCUMENT_ROOT'] . "/incl/myfile.php"); Unless the file needs to be kept outside of where the webserver serves files. Kim, this is exactly what I was looking for. I had been over $_SERVER in the docs.. but somehow missed that basic obvious param. Thanks! Sudeer, Shawn, Michael, your efforts were not wasted either. I absorbed from you all and came up with: define('MY_DOC_ROOT', $_SERVER['DOCUMENT_ROOT']); set_include_path(MY_DOC_ROOT . PATH_SEPARATOR . get_include_path()); include 'MY_php_functions.inc'; which I call with: include $_SERVER['DOCUMENT_ROOT'] . '/MY_inc_php/incsInAllPages.inc'; really basic stuff.. humbling for me, coming from other languages/ environements where I was more expert. ..lucky me this is such a generous and compassionate group of people here. Thanks again -Govinda -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] best way to properly build an include path *regardless* from where I am calling the include?
On Sun, 5 Jul 2009 14:33:07 -0600 Govinda wrote: > I am confusing myself reading the docs just now. > > i.e.: > include_path > basename() > and dirname() > > I had thought from many months ago that > > would include > somefile.php > living in > somedir > regardless from where in the site structure I am calling it. > > Now it does not seem to be doing that. > > What is the safest (portable) way to properly build an include path > *regardless* from where in my site I am calling the include? > If I understand right I can put includes in a dir/ specified by the > include_path setting, but I also want to know how to do this from > just within the root of my virtual server. Like Michael said there is more than one way to deal with this. I personally prefer to use this: require_once ($_SERVER['DOCUMENT_ROOT'] . "/incl/myfile.php"); Unless the file needs to be kept outside of where the webserver serves files. > -Govinda > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > --- Mange venlige hilsner/Best regards Kim Naim Lesmer Programmer/Unix systemadministrator Web: www.bitflop.com E-mail : k...@bitflop.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php