mrtndimitrov Posted September 14, 2009 Share Posted September 14, 2009 Recently I discovered the following strange behavior of 'include' on PHP 5.2 (Windows). According to the documentation and to my understanding 'include' is supposed to simply 'copy' the contents of the file into the calling one. So if we put another 'include' into the included file, the path should be relative to the root file. If you do 'echo getcwd();' from any of the files, the path is indeed relative to the root one. But I discovered that if PHP can not find the file, it searches by making the working directory the included file. Here is the directory structure I set up to test the things: test/index.php {contains: include 'func/main.inc.php';} test/func/main.inc.php {contains: include '1.inc.php'; include 'func/2.inc.php'; include '../func/1.inc.php'; test/func/1.inc.php {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>'; test/func/2.inc.php {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>'; test/func/func/2.inc.php {contains: echo '<h1>'.__FILE__.' - '.getcwd().'</h1>'; The results are: the first include in main.inc.php has no problem finding 1.inc.php the second include in main.inc.php includes test/func/2.inc.php NOT test/func/func/2.inc.php which means PHP is first trying to locate the file based on index.php's working directory the third include prints warnings as it will include './2.inc.php' Is this behavior documented? Can we rely on it? martin Quote Link to comment https://forums.phpfreaks.com/topic/174153-relative-paths-and-include/ Share on other sites More sharing options...
Amtran Posted September 14, 2009 Share Posted September 14, 2009 on PHP 5.2 (Windows) There's your problem! Quote Link to comment https://forums.phpfreaks.com/topic/174153-relative-paths-and-include/#findComment-918048 Share on other sites More sharing options...
Crew-Portal Posted September 14, 2009 Share Posted September 14, 2009 Thats not the problem. This is how PHP is designed to find files. It will always use the first file as the library and try to locate other files from the directory the main file is being executed from. Quote Link to comment https://forums.phpfreaks.com/topic/174153-relative-paths-and-include/#findComment-918049 Share on other sites More sharing options...
PFMaBiSmAd Posted September 14, 2009 Share Posted September 14, 2009 Of course it is documented (though it is less clear in the current description than previous ones) - http://us2.php.net/manual/en/function.include.php An absolute file system path or a relative path using ./ or ../ will use that absolute file system path or that path relative to the main parent file. If you just specify a file name, the include_path is searched to find the file. A dot . in the include_path refers to the current directory, so by having the dot . be first in the include_path, the current directory is always searched first. You may want to consider using $_SERVER['DOCUMENT_ROOT'] to form absolute file system paths - include $_SERVER['DOCUMENT_ROOT'] . '/path_to_file/filename.ext'; Quote Link to comment https://forums.phpfreaks.com/topic/174153-relative-paths-and-include/#findComment-918054 Share on other sites More sharing options...
mrtndimitrov Posted September 14, 2009 Author Share Posted September 14, 2009 Of course it is documented (though it is less clear in the current description than previous ones) - http://us2.php.net/manual/en/function.include.php Sorry but I don't see in the documentation description of such behavior. When in the included file I print the current working directory, it is the parent's one. So, why include '2.inc.php'; is resolved? Quote Link to comment https://forums.phpfreaks.com/topic/174153-relative-paths-and-include/#findComment-918149 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.