Plummer
Plummer

Reputation: 6688

PHP navigating parent folders

I can not find a good example/explanation on how to traverse directories in a PHP script.

I have to call a class from a directory that is a great-great-great...great-great grandparent of the current file.

Since the directory of the file I cam calling is closer to the root than the current php script, I just want to say "c:\folder\script.php".

when I use require_once (dirname('c:/folder/').'script.php'); I get config errors.

This is on IIS. Is the slash direction a factor?

Upvotes: 1

Views: 8659

Answers (2)

Starx
Starx

Reputation: 78981

It might be more suitable to transverse from the root directory.

Generally this is represented by a "/"

require_once("/some/script.php");

Or, define a base directory on the top of executing script and use this as the helper variable on your links. Below is an example:

define("BASEDIR", "../../../../"); // Point to the great great great grans parents just once and use them

Upvotes: 0

Oldskool
Oldskool

Reputation: 34837

Yes, the slash matters between Windows/Linux. That's why the DIRECTORY_SEPARATOR constant was invented, they differ per build. You should be able to:

define('DS', DIRECTORY_SEPARATOR); // Alias to keep it short and readable
require_once('C:' . DS . 'folder' . DS . 'script.php');

Upvotes: 2

Related Questions