Include path messing up include() relative paths

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

Post Reply
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Include path messing up include() relative paths

Post by josh »

Imagine you have this filesystem:

Code: Select all

| ProjectRoot
|--- Folder
    |--- Example.php
    |--- Example2.php
|--- Example.php
Now lets say "project root" was on your include path,

In Example2.php:

Code: Select all

include( 'Example.php' );
This causes ProjectRoot/Example.php to be included instead of ProjectRoot/Folder/Example.php

I tried:

Code: Select all

include( './Example.php' )
( doesnt find the file )

Tried adding '.' to my include path..

The only thing that works is:

Code: Select all

require_once( dirname(__FILE__) . '/Example.php' );
Does PHP expect us to make sure there no like-named files on the include path or always use absolute paths?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Include path messing up include() relative paths

Post by requinix »

It depends how you modified your include path.

Did you forget to put . in it? As the first one?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Include path messing up include() relative paths

Post by josh »

Yeah I did [put it] ( did not forget ), didn't work

Code: Select all

 
 
        set_include_path( '.' . 
            PATH_SEPARATOR . LIBRARY_PATH . '/ZendFramework/library/'
            . PATH_SEPARATOR . LIBRARY_PATH . '/ZendFramework/extras/library/'
            . PATH_SEPARATOR . LIBRARY_PATH . '/DataShuffler/library/'
            . PATH_SEPARATOR . LIBRARY_PATH . '/K12/library/'
            . PATH_SEPARATOR . LIBRARY_PATH . '/php-csv-utils/'
            . PATH_SEPARATOR . LIBRARY_PATH . '/Ne8/library/'
            . PATH_SEPARATOR . LIBRARY_PATH
            . PATH_SEPARATOR . MODULE_PATH
            . PATH_SEPARATOR . APPLICATION_PATH . '/tests/Code/'
            . PATH_SEPARATOR . get_include_path()
        );
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Include path messing up include() relative paths

Post by requinix »

What file did PHP start serving? Because the include path is relative to the current working directory (hint) not to the directory of the running file.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Include path messing up include() relative paths

Post by josh »

getcwd shows the working directory is not the same as the directory the script is in
However I created a testing file in the same directory and include() it from that script using a relative path, the working directory did not have an effect on that.

It would seem setting an include path of "." ( to imply relative from the including file ) has no effect ( even tho theres a comment on the manpage saying it does work ), setting an include path to an absolute path does work however, but is not desired

Here is why it behaves this way, not the working directory FYI "Files are included based on the file path given or, if none is given, the include_path specified"

so if I want to include files safely with least risk of breaking my includes later when I add files to my include path in the future, what is the best practice? The dirname( __FILE__ ) "hack"? That is the million $ question.

FYI

I am in Route.php located @
APPLICATION_PATH . '/tests/Code/Admin/config/Routes.php'

in that file I am including 'Foo.php'

I want it to include

APPLICATION_PATH . '/tests/Code/Admin/config/Foo.php'

instead it goes to

APPLICATION_PATH . '/tests/Code/Foo.php'

I can't put that config/ subdirectory on the include path because then it will break code that is supposed to include the other Foo.php

Personally it would make more sense to me if they did it the other way around, files without paths should always include the file in the current file's path if it exists, because now if I have /config/Bar.php and some other programmer comes in and adds /Bar.php and **** hits the fan that's not cool. Its a security concern IMO because someone could have an application where they inadvertently include a procedural script that performs some highly un-desired action
uyewq
Forum Newbie
Posts: 22
Joined: Thu Sep 17, 2009 6:21 am

Re: Include path messing up include() relative paths

Post by uyewq »

i had the same problem ...

after complexity increases i used my method below. now, i am happy with this method because
it has an advantage to easily transfer your files in future & all links are absolutely will be working without much effort

i include below file to my pages
<?php
/* php include("{$rootfolder}php/examplefile.php");?> */
$rootfolder = "/home/username/public_html/"; // it can be different in your system maybe
/*<link rel="stylesheet" href="<?php echo $belowfolder; ?>css/dis.css" type="text/css">*/
$belowfolder = "/";
?>
in future if i transfer all my portal from root to folder 'transfer' then
$belowfolder becomes = "/transfer/"
$rootfolder becomes = "/home/username/public_html/transfer/";

hope it gives some idea
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Include path messing up include() relative paths

Post by josh »

Yeah I guess the only solution is absolute paths then?
Post Reply