PHP-重定向和包含来自不同目录的文件
假设我有这个给定的目录 公共网页PHP-重定向和包含来自不同目录的文件,php,include,directory,web-hosting,include-path,Php,Include,Directory,Web Hosting,Include Path,假设我有这个给定的目录 公共网页 家 使用者 page1.php 登记册 reg.php page2.php css general.css index.php 现在让我们假设我想在page1.php中包含page2.php 我这样做: include "../page2.php"; 包括“/home/page2.php” 但它不起作用,/应该是根目录(public_html) 或者我想从reg.php重定向到index.php “/index.php”
- 家
- 使用者
- page1.php
- 登记册
- reg.php
- page2.php
- css
- general.css
include "../page2.php";
包括“/home/page2.php”
但它不起作用,/应该是根目录(public_html)
或者我想从reg.php重定向到index.php
“/index.php”不起作用
注意:这些都在托管服务器上。
/
不是站点根目录,而是操作系统文件系统的根目录
您可以使用如下相对路径修饰符:。/../yourincludefile.php
在文件系统中向上导航
或者您可以使用$\u SERVER['DOCUMENT\u ROOT']
从操作系统调用完整路径DOCUMENT\u ROOT
将为您提供更一致的结果,并允许您在目录层次结构中的任何位置都包含相同的文件
例如,如果您有includes/functions.php
,您可以在任何地方、任何脚本上使用它,它将始终包含正确的文件路径:
include_once($_SERVER[DOCUMENT_ROOT]。“/includes/functions.php”)代码>
或者使用字符串中的引用:
include_once($_SERVER[DOCUMENT_ROOT]/includes/functions.php”)
/
不是站点根目录,而是操作系统文件系统的根目录
您可以使用如下相对路径修饰符:。/../yourincludefile.php
在文件系统中向上导航
或者您可以使用$\u SERVER['DOCUMENT\u ROOT']
从操作系统调用完整路径DOCUMENT\u ROOT
将为您提供更一致的结果,并允许您在目录层次结构中的任何位置都包含相同的文件
例如,如果您有includes/functions.php
,您可以在任何地方、任何脚本上使用它,它将始终包含正确的文件路径:
include_once($_SERVER[DOCUMENT_ROOT]。“/includes/functions.php”)代码>
或者使用字符串中的引用:
include_once($_SERVER[DOCUMENT_ROOT]/includes/functions.php”)代码>您应该能够使用相对路径,即:
我想将page2.php包含在page1.php中我这样做:
include "../page2.php";
我想从reg.php重定向到index.php
"../../index.php"
或者,如果您谈论的是public_html中的index.php
"../../../index.php"
您应该能够使用相对路径,即:
我想将page2.php包含在page1.php中我这样做:
include "../page2.php";
我想从reg.php重定向到index.php
"../../index.php"
或者,如果您谈论的是public_html中的index.php
"../../../index.php"
选项1-文件相关:
include("../path/to/file.php");
选项2-根相对
$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/path/to/file.php");
选项1-文件相关:
include("../path/to/file.php");
选项2-根相对
$root = $_SERVER['DOCUMENT_ROOT'];
include($root."/path/to/file.php");