[英]root path doesn't work with php include
/ 在指向根文件夾的鏈接的開頭在 php include 中不起作用。
例如“/example/example.php”
解決辦法是什么?
我假設根文件夾是指您的 Web 文檔根,而不是文件系統根。
為此,您可以
include('example/example.php')
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
我也有這個問題。 保羅迪克森的回答是正確的,但也許這會幫助你理解為什么:
這里的問題是 PHP 是一種服務器端語言。 純 HTML 文檔可以根據您在服務器上設置的根 url 訪問文件(即從您所在的任何子目錄訪問圖像,您將使用/images/example.jpg
從頂級目錄向下),PHP實際上是在使用include (/images/example.jpg)
時訪問服務器根目錄
您設置的站點結構實際上位於 Apache 服務器的文件系統中。 我的站點根目錄看起來像這樣,從服務器根目錄開始向下:
/home2/siteuserftp/public_html/test/
“test”代表您的站點根目錄
所以要回答你的問題,為什么你的 PHP 包含沒有得到你想要的結果(它完全按照它應該的方式工作)是因為你要求 PHP 代碼嘗試在服務器根目錄中找到你的文件,而實際上它是位於您網站的 HTML 根目錄中,類似於上面的內容。
您的文件將基於“test/”的站點根目錄,如下所示:
/home2/siteuserftp/public_html/test/about/index.php
Paul Dixon 給出的答案是:
include($_SERVER['DOCUMENT_ROOT'].'/example/example.php')
正是解決您的問題的方法(不要擔心試圖找到文檔根目錄來替換“DOCUMENT_ROOT”,PHP 會為您完成。只要確保您在其中包含“DOCUMENT_ROOT”即可)
編輯:
可以在此處找到更多信息 DOCUMENT_ROOT 和其他 PHP SERVER 變量
include()(以及許多其他函數,如 require()、fopen() 等)都在本地文件系統上工作,而不是在 web 根目錄下。
所以,當你做這樣的事情時
include( "/example/example.php" );
您正在嘗試從 *nix 機器的根目錄中包含。
雖然有多種方法可以處理您正在做的事情,但 Paul Dixon 的建議可能是您最好的選擇。
每個 Web 服務器都有一個public_html
文件夾,您通常將文件等保存在其中。通過使用/
,您將無法訪問public_html
,而是直接指向主(無法訪問的)根目錄。 因此,請改用$_SERVER['DOCUMENT_ROOT']."/your/locati.on"
我在運行 Windows 和 IIS 的機器上解決了這個問題:
<?php
$docroot = 'http://www.example.com/';
include ($docroot.'inc-header.php');
?>
如果您在本地開發機器上,您可以通過在 C:\\Windows\\System32\\drivers\\etc\\hosts 中添加以下內容來強制您的域指向 localhost
127.0.0.1 www.example.com
此外,您需要像這樣在 php.ini 中啟用 allow_url_include
allow_url_include = On
對我來說,以下技巧奏效了。 我使用帶有 IIS 的 Windows,所以 DOCROOT 是 C:\\Inetpub\\wwwroot。
subst W: C:\Inetpub\wwwroot
include_path = ".;c:\php\active\includes;W:\" doc_root = W:\
現在,兩個版本都允許:
include '/common/common.inc'; // access to mapped W: root include 'common/common.inc'; // access to W: within include_path
某些版本的 PHP 可能在文檔根目錄的末尾有分隔符,而其他版本可能沒有。 作為一個實際問題,您可能想要使用:
$r = trim(filter_input(INPUT_SERVER, 'DOCUMENT_ROOT', FILTER_SANITIZE_STRING));
if (substr($r, 0, 1) == '/')
{
define("PATCH_SEPARATOR", "/");
}
else
{
define("PATCH_SEPARATOR", "\\");
}
if (substr($r, -1) == PATCH_SEPARATOR)
{
include_once ($r . 'example/example.php');
}
else
{
include_once ($r . PATCH_SEPARATOR . 'example/example.php');
}
也許這有點不合常規
如果我有這樣的案例
/var/www/namedir/ <= root
/var/www/namedir/example/example.php <= file to include
-- directory when i need the include --
/var/www/namedir/dir1/page.php
/var/www/namedir/dir1/dirA/page.php
/var/www/namedir/dir1/dirB/page.php
我使用的解決方案很簡單。 獲取“Dir1”之前的路徑
像這樣的東西
include (substr(dirname(__FILE__),0,strpos(dirname(__FILE__), '/dir1'))."/example/example.php");
我發現它很有用,我需要重命名主子目錄,例如從
/var/www/namesite/internalDirA/dirToInclude/example.php
/var/www/namesite/internalDirA/dir1/dirA/example.php
/var/www/namesite/internalDirA/dir1/dirB/example.php
到
/var/www/namesite/dirReserved/dirToInclude/example.php
/var/www/namesite/dirReserved/dir1/dirA/example.php
/var/www/namesite/dirReserved/dir1/dirB/example.php
這個答案並不是真正針對根目錄,但一種解決方法是使用../
跳轉到父目錄。
當然,您需要知道這種方法的文件結構。
例如,您可以使用:
include('../parent.php');
include('../../grand_parent.php');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.