-1

I'm trying to include file on ubuntu but I have a weird problem. The including file is in this path - /var/www/cms-dev/corefiles/classes/config.hp The file i'm trying to include is this file - /var/www/cms-dev/corefiles/lang.php

For some reason, while trying to include it it says that the file dosen't exist -

Warning: require_once(../lang.php) [function.require-once]: failed to open stream: No such file or directory in /var/www/cms-dev/corefiles/classes/config.php on line 9

SO for looking a solution for this I used scandir to scan the current directory and the upper directory.

For the current directory I got this using scandir("./") -

Array ( [0] => . [1] => .. [2] => .htaccess [3] => admin [4] => ckeditor [5] => corefiles [6] => index.php [7] => install.php [8] => log.txt )

For the current directory using scandir("/") I got this -

Array ( [0] => . [1] => .. [2] => bin [3] => boot [4] => cdrom [5] => dev [6] => etc [7] => home [8] => host [9] => initrd.img [10] => initrd.img.old [11] => lib [12] => lib64 [13] => lost+found [14] => media [15] => mnt [16] => opt [17] => proc [18] => root [19] => run [20] => sbin [21] => selinux [22] => srv [23] => sys [24] => tmp [25] => usr [26] => var [27] => vmlinuz [28] => vmlinuz.old )

For scanning the upper directory using scandir("../") I got this -

Array ( [0] => . [1] => .. [2] => cms-dev [3] => index.html [4] => info.php [5] => test.txt [6] => test.txt~ )

But config.php exists in /var/www/cms-dev/corefiles/classes/config.php I include it here - index.php and here urlHandler.php , but shouldn't config.php include the files relativley to himself?

Yehonatan
  • 3,168
  • 7
  • 31
  • 39
  • If you use absolute paths, you have no problem, but with relative paths you have to take care... The relative path is always based on the file (location) executed. – thedom Jul 27 '12 at 14:01
  • `./` is the same directory, relative to the current file. `../` is the ascendant directory to your current directory (relative to your working file). `/` is the root directory to which you have access. There is no `../lang.php` from whatever file you're trying to bring in the lang file. – MetalFrog Jul 27 '12 at 14:02
  • @thedom - it's really based on the include_path setting for relative paths, which typically has . as the first entry in the pathlist – Mark Baker Jul 27 '12 at 14:07

2 Answers2

2

Your answer is clear as day. When you scan the upper directory using scandir("../") you get:

Array ( [0] => . [1] => .. [2] => cms-dev [3] => index.html [4] => info.php [5] => test.txt [6] => test.txt~ )

lang.php isn't in the directory that you think it's in, in relation to the file that's calling it. Try absolute paths.

Matt
  • 6,993
  • 4
  • 29
  • 50
  • @Yehonatan store the docroot for your site in a variable (`define('DOCROOT', $_SERVER['DOCUMENT_ROOT'] . "/")`), then refer to the file as it is in relation to the document root (`require_once(DOCROOT . "corefiles/lang.php");`) - http://php.net/manual/en/reserved.variables.server.php – Matt Jul 27 '12 at 14:06
  • @Yehonatan the docroot is your root directory, referred to by its file structure, as opposed to its URL (e.g.: `/htdocs/mysite/public_html/` vs `http://www.mysite.com/`) – Matt Jul 27 '12 at 14:22
2

Are you requiring config.php in index.php and urlHandler.php? If so, remember that "the path for nested require_once() is always evaluated relative to the called / first file containing require_once()".

In other words, no, config.php should not include files relative to itself, but rather relative to index.php (and thus also urlHandler.php, if they're in the same directory).

See http://www.php.net/manual/en/function.require-once.php for more information.

Don
  • 863
  • 1
  • 8
  • 22