Goaler444
Goaler444

Reputation: 2621

PHP functions and File Paths

My website contains multiple files organized in directories. More specifically, this is my structure:

If you have trouble understanding this or require more information, add a comment

Within my functions.php, I create a link to refer to my index file. Since this function is called by all my main .php files, the path to the index file varies dependent on the page calling the function.

For example, if the function is called by my index file, the path would obviously be "index.php". However if the function is called from my viewaccount.php file, the required path would be "../../index.php"

Therefore my problem is:

How can I set a general path, which independent from which file I call my function, it will always link correctly to my index file.

Once quick fix is to put all my main php files as siblings to my index, but I want to keep all my files organised

Upvotes: 0

Views: 125

Answers (2)

Paul Dessert
Paul Dessert

Reputation: 6389

Something like this:

require_once($_SERVER['DOCUMENT_ROOT'] . '/src/php/functions.php');

Upvotes: 2

slash28cu
slash28cu

Reputation: 1634

The way most of the php frameworks work is similar to the following, zend framework works like this:

Declare a constant APPLICATION_PATH and then make all the paths relative to this one.

define("APPLICATION_PATH", "/var/www/mysite");

And then all your requires will be relative to your APPLICATION_PATH.

require_once APPLICATION_PATH ."/system/helpers/{$helper}.php";

* With this approach you can include files from whatever script without issues. Because all the paths are going to be relative to your APPLICATION_PATH.

Upvotes: 2

Related Questions