Path problem

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
berryz
Forum Newbie
Posts: 5
Joined: Mon Nov 13, 2006 8:42 am

Path problem

Post by berryz »

I have encountered a path problem that is really strange. When I request a file the path i specify has to start from root dir. Look at the example:
I have:
root\conf\setup.php
root\class\defectBook.class.php
I want to access 'setup.php' from 'defectbook.class.php'. so the code might be:
------------------------------------------
(root\class\defectBook.class.php)
<?php

require ('..\conf\setup.php')
?>
-------------------------------------------
However, it won't work. It only works with absolute path, ie. 'conf\setup.php'. Relative path such as "..\" or "\" just dont work. Is it a configuration issue or something else?

I'm using windows version apache with php.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

please try

Code: Select all

<?php
echo "<pre>\n";
echo 'file: ', __FILE__, "\n";
echo 'cwd: ', getcwd(), "\n";
echo 'realpath(..): ', realpath('..'), "\n";
echo 'realpath(../conf): ', realpath('../conf'), "\n";
echo "</pre>";

require '../conf/setup.php'
?>
and post the output.
What's the absolute path of setup.php?
berryz
Forum Newbie
Posts: 5
Joined: Mon Nov 13, 2006 8:42 am

Post by berryz »

The results are:
----------------------------------------------------------------------------------------------------------
file: F:\Dev\test.php
cwd: F:\Dev
realpath(..): F:\
realpath(../conf):


Warning: require(../conf/setup.php) [function.require]: failed to open stream: No such file or directory in F:\Dev\test.php on line 10

Fatal error: require() [function.require]: Failed opening required '../conf/setup.php' (include_path='.;c:\php\includes;F:\Dev\smarty\libs') in F:\Dev\test.php on line 10
-----------------------------------------------------------------------------------------------------------
the absolute path is F:\Dev\conf\setup.php, my root dir is F:\Dev

Thanks for helping.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

relative paths refer to the current working directory.
berryz wrote:cwd: F:\Dev
...
the absolute path is F:\Dev\conf\setup.php
F:/Dev + conf/setup.php -> F:/Dev/conf/setup.php
try

Code: Select all

require 'conf/setup.php';
berryz
Forum Newbie
Posts: 5
Joined: Mon Nov 13, 2006 8:42 am

Post by berryz »

I tried again with the 'test.php' in 'conf' dir. The output is different now:
--------------------
file: F:\Dev\conf\test.php
cwd: F:\Dev\conf
realpath(..): F:\Dev
realpath(../conf): F:\Dev\conf


Fatal error: Using $this when not in object context in F:\Dev\conf\setup.php on line 3
--------------------
This result suggests that it has successfully found the 'setup.php' using '../conf/' i think?
This is how i expected the path to work as normally it does...dunno whats wrong with my script ;x
berryz
Forum Newbie
Posts: 5
Joined: Mon Nov 13, 2006 8:42 am

Post by berryz »

Ok i think i understand now. It's just nested 'require' casued problem because the file that is requiring 'conf\setup.php' is in 'F:\Dev\', so when '..\' applied it still directs to '\root' hence file not found.

The question now becomes how do I appoint '\' to my root dir, which is 'F:\Dev', instead of 'F:\" ? Currently '\' represents 'F:\'.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

include/require does not change the cwd. If your initial script is located in F:\Dev (and cwd=F:\Dev) and you include F:\Dev\yadda\script.php the cwd still is F:\Dev and relativ paths refer to F:\Dev.
berryz
Forum Newbie
Posts: 5
Joined: Mon Nov 13, 2006 8:42 am

Post by berryz »

Yes I got that, thanks.

I just noticed that currently to me '\' represents 'F:\' rather than "F:\Dev|" which is my root dir. Is there a way I can appoint '\' to my root dir?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

No, it refers to the local filesystem, not to document_root of the webserver.
Post Reply