[英]root path for php files in Linux apache
I am having problems with locating files with php include
on my Ubuntu server. 我在使用我的Ubuntu服务器上的php include
定位文件时遇到问题。
structure of site 网站结构
/var/www/
home/index.php
include/header.php
When I try to insert the following include_once('/include/header.php')
in the file home/index.php it does not work. 当我尝试在home / index.php文件中插入以下include_once('/include/header.php')
,它不起作用。
however if I change that to include_once('../include/header.php')
, it works fine, but to have consistency through out my site I can't allow this 但是,如果我将其更改为include_once('../include/header.php')
,它可以正常工作,但为了在我的网站中保持一致性,我不能允许这样做
What is the best way to fix this? 解决这个问题的最佳方法是什么?
If your document root is /var/www/
then you can use: 如果您的文档根目录是/var/www/
那么您可以使用:
include $_SERVER['DOCUMENT_ROOT'] . '/include/header.php';
Typically, PHP gets the DOCUMENT_ROOT
correct so you can usually rely on that. 通常,PHP会使DOCUMENT_ROOT
正确,因此您通常可以依赖它。
If you want to include the file relative to the script that is doing the include, you can do: 如果要包含相对于执行include的脚本的文件,可以执行以下操作:
include dirname(__FILE__) . '/../include/header.php';
The leading slash indicates an absolute path. 前导斜杠表示绝对路径。 Most applications will define an APP_ROOT
constant or something similar early in the cycle to solve these problems. 大多数应用程序将在周期的早期定义APP_ROOT
常量或类似的东西来解决这些问题。
define('APP_ROOT', dirname(__FILE__));
Then later on you can include files with: 然后,您可以包含以下文件:
include(APP_ROOT . '/includes/header.php');
I would try the following 我会尝试以下方法
include_once($_SERVER['DOCUMENT_ROOT'].'/include/header.php'); include_once($ _ SERVER [ 'DOCUMENT_ROOT'] '/包括/ header.php文件');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.