PHP Includes failing to include file from subdirectory?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

PHP Includes failing to include file from subdirectory?

Post 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");}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Re: PHP Includes failing to include file from subdirectory?

Post 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");
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

That works great! Thanks! How come my original method worked in other cases though?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

If you mess with your include, may I suggest you use the PATH_SEPARATOR constant for extensibility.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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?
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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?
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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);
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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);
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post 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.
Post Reply