簡體   English   中英

PHP include不能正常工作

[英]PHP include doesn't work as it should

我在Debian Squeeze上的/home/www/example.com/www中的http://www.example.com/擁有文檔。

/home/www/example.com/
    www/
         index.php
    php/
         include_me.php

在php.ini中,我已取消注釋並更改為:

include_path =".:/home/www/example.com"

在www的腳本index.php中,我具有require_once("/php/include_me.php") 我從PHP獲得的輸出是:

Warning: require_once(/php/include_me.php) [function.require-once]: failed to open stream: No such file or directory in /home/www/example.com/www/index.php on line 2

Fatal error: require_once() [function.require]: Failed opening required '/php/include_me.php' (include_path='.:/home/www/example.com') in /home/www/example.com/www/index.php on line 2

如您所見,include路徑已根據錯誤正確設置。 但是如果我確實需要require_once("../php/include_me.php"); , 有用。 因此,包含路徑一定有問題。

有誰知道我能做些什么來解決它?

php/

是相對路徑,並使用當前目錄作為起點

./php/

是相對路徑,並顯式聲明當前目錄( )作為起點

/php/

不是相對路徑,這意味着它的起點是頂層目錄(根目錄/

從文檔上include

如果定義了路徑(無論是絕對路徑(在Windows中以驅動器號或\\開頭,在Unix / Linux系統上以/開頭)還是相對於當前目錄(以。或..開頭),則include_path將被完全忽略。 例如,如果文件名以../開頭,則解析器將在父目錄中查找以找到請求的文件。

由於在這種情況下要指定絕對路徑,所以將忽略include_path

我同意Mihai Stancu的觀點,但我想補充一點,包括使用dirname(__ FILE__)可能是更好的做法,以便在目錄周圍移動代碼時不會中斷。 這將像一條絕對路徑,但可以將它們視為本地路徑。

對require_once(“ / php / include_me.php”)的說明:

您已為/home/www/example.com/設置了包含路徑,但這是/ home / www的子目錄。 您的require_once正在尋找/php/include_me.php。

/
home/
    www/
        example.com/
              php/
              www/
php/
    include_me.php

您的要求調用中有一個/,它正在尋找/php/include_me.php。 要查找/home/www/example.com/php/include_me.php,您需要:

require_once('php/include_me.php');

您還可以將包含路徑設置為:

include_path =".:/home/www/example.com/:/home/www/example.com/www/:/home/www/example.com/php/"

您一次需要的開始斜杠將從根開始搜索...嘗試:

require_once('php/include_me.php');

這試圖從字面上獲取文件系統/ php上的文件,而不是Web目錄。

require_once("/php/include_me.php");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM