Reputation: 1959
The structure
libs/functions.php
test/b.php
a.php
content.html
functions.php
<?php
function showContent() {
return file_get_contents('content.html');
}
a.php
<?php
include 'libs/functions.php';
echo showContent();
b.php
<?php
include '../libs/functions.php';
echo showContent();
content.html
test
a.php will show the word test, but b.php is in the test folder, therefore the file path will be wrong, how to fix it?
Upvotes: 1
Views: 275
Reputation: 5133
I don't know why you need to do this but one solution would be to create an index with the all php files from the folders you want.
Another solution may be to set a constant path from the base path. You can get the root of the website using $_SERVER['DOCUMENT_ROOT']
. This is the fastest solution and by setting this constant you will always refer directly functions.php file.
Upvotes: 0
Reputation: 756
You can use document root as start of your path so you never have to worry about it.
$_SERVER['DOCUMENT_ROOT'].'/libs/functions.php'
and
$_SERVER['DOCUMENT_ROOT'].'/content.html'
Upvotes: 0
Reputation: 8020
You are thinking about this from the wrong direction.
Instead, of opening the different php files, create one file index.php
that includes
and works with the scripts you need. That way you will always be at the root and will be able to access the files/functions you need.
Another way is cloning the directory in sub-directories, but I do NOT suggest that option. You will just get confused over time.
Upvotes: 0
Reputation: 870
please use the following:
$basePath = dirname(__FILE__);
use this $basePath
prefixing to any include or require statement.
I hope this would help
Upvotes: 1