PHP - 包含固定路径 [英] PHP - Include fixed path

查看:117
本文介绍了PHP - 包含固定路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


已更新 - 已编辑

Updated - Edited

我想使用来自不同目录的菜单PHP文件。

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 /).

问题是自动添加从菜单路径之前调用(包含)文件的目录。我不想要,我想要的路径是一样的所有文件夹,我从那里我没有menu.php

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

看到这个例子。一线,有两类。 / categories / categories /\".

推荐答案

使用 include_once 可能会更方便,因为这样做只能包含一次。

Using include_once might be more convenient, as this guarantees that it's only included once.

此外,你最好使用绝对路径( include /menu.php; )。您正在使用相对路径( includemenu.php; )。

Furthermore, you'd be best off using absolute paths (include "/menu.php";). You are currently using a relative path (include "menu.php";).

由于PHP是一种服务器端语言,您可能需要从根路径中包含它,这意味着您会得到一个看起来像 /home/username/public_html/menu.php 。最简单的方法是包括你的menu.php,如下所示: include $ _SERVER ['DOCUMENT_ROOT']。/ 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";.

要找到您的 menu.php 所在的确切路径(因此,请从您的路径中找出需要包含它),在你的menu.php中运行 echo $ _SERVER [SCRIPT_FILENAME]; ,因为它可能是不同于 $ _SERVER ['DOCUMENT_ROOT']

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'].

这篇关于PHP - 包含固定路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆