tags:

views:

81

answers:

3

Hi all, got a strange problem that has so far left me scratching my head.

I have an index page with a common.php require file a few folders away. In the require file there are six scripts, with all but two of them being in the same folder as common.php.

So for the scripts in the same folder I simply use the file name eg-require('filename.php'), which works fine. The two remaining scripts are in an adjacent folder named config so I try using require('../config/file.php') but it doesn't work.

The strange thing is if I use the relative path from the index file it works?

How comes I can use paths that are relative to common.php if the files are in the same folder but not when in a different folder?

I'm sure there's a logical explanation that escapes me right now so any help would be greatly appreciated.

thanks J

A: 

is the folder containing common.php part of your includes-path by any chance? ie, if in the index page you just require('common.php') instead of the relative path to common.php does it still pick it up?

Mailslut
using just require('common.php') doesn't work.This the structure in index.phprequire('folder1/folder2/common.php')Then in common its as above. Four files are in folder2 and two are in folder3. Can't think why it picks up the files in folder2 but not folder3 using ../folder3/file.php?
Taylor
+1  A: 

First set the path like this

set_include_path(get_include_path() . PATH_SEPARATOR . realpath("./")); //change accordingly the path e.g. path to common.php

and then use the file required in your script.

Gaurav Sharma
haven't used include_paths. Was wondering why 4 of the files can use relative paths from the parent (common.php) and the other two use relative paths from index.php (not the parent)?Does using require('folder1/folder2/common.php') set folder2 as an include_path?
Taylor
If you write the above code in common.php then the include path will be set to the path of common.php
Gaurav Sharma
this does work but its not really what I'm after. I just don't get why I can't go backwards relative to common.php but can go forwards. For instance, I can access folder4/file that is within the same folder as common.php by using 'folder4/file'. But if folder4 were on the same level as the common.php folder I can't access it using '../folder4/file'.
Taylor
+1  A: 

As far as I understand, you are including files inside included files. As far as I think, the translation of "../" depends on the current working directory, not the directory in which the included file resides -- but I am not sure. Anyway, you can workaround the problem inside the included files with something along these lines:

Assuming:

  • index.php file resides in C:/inetpub/wwwroot/project1
  • inc01.php file resides in C:/inetpub/wwwroot/project1/include/
  • cfg01.php file resides in C:/inetpub/wwwroot/project1/config/

index.php contains:

require( "include/inc01.php" );

To include cfg01.php inside inc01.php regardless of whether the container script is /index.php, /admin/index.php, /products/laptops/index.php:

require( dirname( dirname( __FILE__ ) ) . "/config/cfg01.php" );

First dirname returns the directory name in which inc01.php exists. Second one returns its parent directory. You can use the constant DIRECTORY_SEPARATOR instead of worrying whether to use forward or back slashes. You can cascade dirname three times to get ../../ and so on.

Salman A
FWIW, you can make it a little easier by saying "require(dirname(__FILE__) . "/../config/cfg01.php")
Narcissus