Updated - Edited
I want to use a menu PHP file from different directories.
The problem is when i call the file. Example:
Include menu.php from categories folder:http://localhost/admin/categories/categories/cars Include
Include menu.php from index folder: http://localhost/admin/categories/cars
Note that the menu file is not in a folder (index /).
The problem is add automatically the directory from where i call (inlude) the file before the menu's path. I do not want that, i want the path to be the same for all folder from where i inlude the menu.php
See the example. On first line, there two categories. "/categories/categories/".
Using include_once
might be more convenient, as this guarantees that it's only included once.
Furthermore, you'd be best off using absolute paths (include "/menu.php";
). You are currently using a relative path (include "menu.php";
).
As PHP is a serverside language, you may need to include it from the root-path, meaning that you'd get a path that looks like /home/username/public_html/menu.php
. The easiest way to go about this would be to include your menu.php like this: include $_SERVER['DOCUMENT_ROOT']."/menu.php";
.
To find the exact path where your menu.php
is located (and thus, find out from what path you need to include it from), run echo $_SERVER["SCRIPT_FILENAME"];
in your menu.php, as it may be a different path than $_SERVER['DOCUMENT_ROOT']
.
Use an absolute path instead of a relative.
Example: include('/data/web/123456/html/settings/settings.php');
instead of include('settings.php');
Additionally i think the require_once() function would be more suitable here.
You have 2 options: 1. @rocket gave you the probable answer to your questions but you ignored it: If you include the file like '/menu.php' mind the '/' it will include a menu.php file that relies on the root folder. Adapt it to your needs, but don't forget the slash.
2. How about using dirname(__DIR__)
as path folowed by the name of your file. In your case:
include dirname(__DIR__).'/menu.php'