Page 1 of 2

PHP Includes failing to include file from subdirectory?

Posted: Fri Jun 01, 2007 3:58 pm
by JAB Creations
How would this fail to open a subdirectory? PHP is telling me it's not looking up a directory and then in the templates directory but rather it's looking inside the same directory as the file this code is on?

Code: Select all

{include("../templates/includes.php");}

Re: PHP Includes failing to include file from subdirectory?

Posted: Fri Jun 01, 2007 4:00 pm
by Chris Corbyn
JAB Creations wrote:How would this fail to open a subdirectory? PHP is telling me it's not looking up a directory and then in the templates directory but rather it's looking inside the same directory as the file this code is on?

Code: Select all

{include("../templates/includes.php");}
The path is relative the script you're calling in your web browser, not the file that has the include() statment. If you want a path relative to the actual file you need to do this:

Code: Select all

include realpath(dirname(__FILE__) . "/../templates/includes.php");

Posted: Fri Jun 01, 2007 4:08 pm
by JAB Creations
That works great! Thanks! How come my original method worked in other cases though?

Posted: Fri Jun 01, 2007 4:18 pm
by Chris Corbyn
JAB Creations wrote:That works great! Thanks! How come my original method worked in other cases though?
Because all your files were probably included from the same directory ;)

Posted: Fri Jun 01, 2007 4:34 pm
by Ollie Saunders
I don't believe the call to realpath() is necessary. As I can alternative you might want to try

Code: Select all

set_include_path('.:' . dirname(__FILE__));
or

Code: Select all

set_include_path(get_include_path() . ':' . dirname(__FILE__));
or something like that.

Posted: Fri Jun 01, 2007 5:01 pm
by RobertGonzalez
If you mess with your include, may I suggest you use the PATH_SEPARATOR constant for extensibility.

Posted: Fri Jun 01, 2007 5:38 pm
by Ollie Saunders
Actually this is an issue I'd like to bring up, perhaps I'm just not getting something or I've forgotten something I used to know but, how exactly does the PATH_SEPARATOR constant improve extensibility?

Posted: Fri Jun 01, 2007 5:43 pm
by Chris Corbyn
ole wrote:Actually this is an issue I'd like to bring up, perhaps I'm just not getting something or I've forgotten something I used to know but, how exactly does the PATH_SEPARATOR constant improve extensibility?
I've avoided bringing this up for fear of looking stoopid. But yes indeed, it seems like a pointless constant. PHP handles paths with "/" on *all* platforms. You should just stick with a slash for readability. Like ~ole says though, am I missing something?

Posted: Fri Jun 01, 2007 5:46 pm
by RobertGonzalez
What happens if future release change? The argument that '/' is treated the same on all platforms could be easily transposed onto the argument that $HTTP_SERVER_VARS will be a standard in the PHP core, or that short tags will always be available.

I know it seems like right now there is no need for it. But that is almost like saying three years ago that there really is no need to use <?php since PHP knows what to do with <?. Know what I mean?

Posted: Fri Jun 01, 2007 5:59 pm
by Ollie Saunders
What happens if future release change?
I don't think you can reasonably protect yourself from unpublished changes to the language itself. I mean, when does it end? They could potentially decide to change anything.

If it was published that the path separator might change I would definitely be using it. I wouldn't mind so much but its such a long name as well.

Posted: Fri Jun 01, 2007 6:58 pm
by superdezign

Code: Select all

include('..' . PATH_SEPARATOR . '..' . PATH_SEPARATOR . 'classes' . PATH_SEPARATOR . 'sql' . PATH_SEPARATOR . 'sql.php');
I don't think I'd ever want to use includes anymore.

Posted: Fri Jun 01, 2007 7:07 pm
by John Cartwright
d11wtq wrote:
ole wrote:Actually this is an issue I'd like to bring up, perhaps I'm just not getting something or I've forgotten something I used to know but, how exactly does the PATH_SEPARATOR constant improve extensibility?
I've avoided bringing this up for fear of looking stoopid. But yes indeed, it seems like a pointless constant. PHP handles paths with "/" on *all* platforms. You should just stick with a slash for readability. Like ~ole says though, am I missing something?
What about macs? DIRECTORY_SEPERATOR will contain whatever the the OS specific directory seperator is whether it is /, \, :, etc

I'm still not totally convinced myself, though.
superdezign wrote:

Code: Select all

include('..' . PATH_SEPARATOR . '..' . PATH_SEPARATOR . 'classes' . PATH_SEPARATOR . 'sql' . PATH_SEPARATOR . 'sql.php');
I don't think I'd ever want to use includes anymore.
Considering the length of the constant, I feel your pain, which is why I usually redefine the constant to a more compact version.

Code: Select all

define('DS', DIRECTORY_SEPERATOR);

Posted: Fri Jun 01, 2007 7:16 pm
by superdezign
Jcart wrote:

Code: Select all

define('DS', DIRECTORY_SEPERATOR);
Much too painless.

Code: Select all

define('I_SEPARATE_PATHS_THE_PROPER_WAY', PATH_SEPARATOR);

Posted: Fri Jun 01, 2007 7:31 pm
by Ollie Saunders
Just like to clarify a bit of confusion: PATH_SEPARATOR has the value of ':' and is used for separating multiples paths like in the args to set_include_path(). So this is incorrect:
include('..' . PATH_SEPARATOR . '..' . PATH_SEPARATOR . 'classes' . PATH_SEPARATOR . 'sql' . PATH_SEPARATOR . 'sql.php');
and evaluates to

Code: Select all

include('..:..:classes:sql:sql.php');
What about macs? DIRECTORY_SEPERATOR will contain whatever the the OS specific directory seperator is whether it is /, \, :, etc
For this reason DIRECTORY_SEPARATOR has its uses. But I'm trying to asertain whether PHP cares this. I have a hunch it's not bothered either way. The manual isn't particularly clear.

Posted: Fri Jun 01, 2007 7:43 pm
by maliskoleather
ole wrote:Just like to clarify a bit of confusion: PATH_SEPARATOR has the value of ':' and is used for separating multiples paths like in the args to set_include_path().
I was just about to point that out.

I'm not too sure on the reasons why to use PATH_SEPERATOR, but thats what I was taught to use. I always just assumed that for some reason there were some OS's that it was different on.
ole wrote:For this reason DIRECTORY_SEPARATOR has its uses. But I'm trying to asertain whether PHP cares this.
As far as im aware with this, PHP uses '/' on all platforms. I only use DIRECTORY_SEPARATOR when i have to pass a filepath off to some external program.