Path problem
Moderator: General Moderators
Path problem
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.
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.
please tryand post the output.
What's the absolute path of setup.php?
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'
?>What's the absolute path of setup.php?
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.
----------------------------------------------------------------------------------------------------------
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.
relative paths refer to the current working directory.
try
F:/Dev + conf/setup.php -> F:/Dev/conf/setup.phpberryz wrote:cwd: F:\Dev
...
the absolute path is F:\Dev\conf\setup.php
try
Code: Select all
require 'conf/setup.php';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
--------------------
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
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:\'.
The question now becomes how do I appoint '\' to my root dir, which is 'F:\Dev', instead of 'F:\" ? Currently '\' represents 'F:\'.