Page 1 of 1

Include path messing up include() relative paths

Posted: Tue Sep 22, 2009 8:35 pm
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?

Re: Include path messing up include() relative paths

Posted: Wed Sep 23, 2009 12:25 am
by requinix
It depends how you modified your include path.

Did you forget to put . in it? As the first one?

Re: Include path messing up include() relative paths

Posted: Wed Sep 23, 2009 12:57 am
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()
        );

Re: Include path messing up include() relative paths

Posted: Wed Sep 23, 2009 1:42 am
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.

Re: Include path messing up include() relative paths

Posted: Thu Sep 24, 2009 3:17 am
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

Re: Include path messing up include() relative paths

Posted: Thu Sep 24, 2009 8:17 am
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

Re: Include path messing up include() relative paths

Posted: Thu Sep 24, 2009 6:46 pm
by josh
Yeah I guess the only solution is absolute paths then?