[英]How to include a file that has includes (Including nested includes)
我試圖讓 SSI 為我的 SMF 論壇工作,但我這樣做的方式似乎與預期的方法不同。 我想include
來自不同目錄的子目錄的SSI.php
文件。 In other words, the SSI.php
file is located at /home/account/public_html/forums/SSI.php
and the file that says include
SSI.php
is at /home/account/public_html/dev/extension/page/file.php
。
如果我把include "/home/account/public_html/forums/SSI.php";
我明白了
require_once(/QueryString.php): failed to open stream: No such file or
directory in '/home/account/public_html/forums/SSI.php line 64'
如果我把它作為相對路徑,我會得到同樣的結果。
我試過chdir
和$_SERVER["DOCUMENT_ROOT"]
,結果相同。
SSI.php
(出現錯誤的地方)的開頭是:
<?php
/**
* Simple Machines Forum (SMF)
*
* @package SMF
* @author Simple Machines http://www.simplemachines.org
* @copyright 2011 Simple Machines
* @license http://www.simplemachines.org/about/smf/license.php BSD
*
* @version 2.0.17
*/
// Don't do anything if SMF is already loaded.
if (defined('SMF'))
return true;
define('SMF', 'SSI');
// We're going to want a few globals... these are all set later.
global $time_start, $maintenance, $msubject, $mmessage, $mbname, $language;
global $boardurl, $boarddir, $sourcedir, $webmaster_email, $cookiename;
global $db_server, $db_name, $db_user, $db_prefix, $db_persist, $db_error_send, $db_last_error;
global $db_connection, $modSettings, $context, $sc, $user_info, $topic, $board, $txt;
global $smcFunc, $ssi_db_user, $scripturl, $ssi_db_passwd, $db_passwd, $cachedir;
global $image_proxy_enabled, $image_proxy_secret, $image_proxy_maxsize;
global $auth_secret, $cookie_no_auth_secret;
// Remember the current configuration so it can be set back.
$ssi_magic_quotes_runtime = function_exists('get_magic_quotes_gpc') && get_magic_quotes_runtime();
if (function_exists('set_magic_quotes_runtime'))
@set_magic_quotes_runtime(0);
$time_start = microtime();
// Just being safe...
foreach (array('db_character_set', 'cachedir') as $variable)
if (isset($GLOBALS[$variable]))
unset($GLOBALS[$variable]);
// Get the forum's settings for database and file paths.
require_once(dirname(__FILE__) . '/Settings.php');
// Make absolutely sure the cache directory is defined.
if ((empty($cachedir) || !file_exists($cachedir)) && file_exists($boarddir . '/cache'))
$cachedir = $boarddir . '/cache';
$ssi_error_reporting = error_reporting((defined('E_STRICT') ? E_ALL | E_STRICT : E_ALL) & ~E_DEPRECATED);
/* Set this to one of three values depending on what you want to happen in the case of a fatal error.
false: Default, will just load the error sub template and die - not putting any theme layers around it.
true: Will load the error sub template AND put the SMF layers around it (Not useful if on total custom pages).
string: Name of a callback function to call in the event of an error to allow you to define your own methods. Will die after function returns.
*/
$ssi_on_error_method = false;
// Don't do john didley if the forum's been shut down competely.
if ($maintenance == 2 && (!isset($ssi_maintenance_off) || $ssi_maintenance_off !== true))
die($mmessage);
// Fix for using the current directory as a path.
if (substr($sourcedir, 0, 1) == '.' && substr($sourcedir, 1, 1) != '.')
$sourcedir = dirname(__FILE__) . substr($sourcedir, 1);
// Load the important includes.
require_once($sourcedir . '/QueryString.php');
require_once($sourcedir . '/Subs.php');
require_once($sourcedir . '/Errors.php');
require_once($sourcedir . '/Load.php');
require_once($sourcedir . '/Security.php');
File.php
要簡單得多,包括:
<?php
include "/home/account/public_html/forums/SSI.php";
echo $sourcedir;
?>
由於此代碼將用於各種不同的安裝,並非所有安裝都允許編輯SSI.php
,因此我希望有一種無需編輯即可使其工作的方法SSI.php
。 任何東西都可以放在file.php
中。
編輯:不是權限問題。 isreadable() 對所有三個文件都返回 true,我可以毫無問題地對它們 append。
看來您在已經包含使用相對路徑的文件中使用了require
/ require_once
,因此解決方案是使用絕對路徑。
注意: __DIR__
常量將是定義文件絕對路徑的好方法,如果在include
中使用,請參見
所以嘗試將$sourcedir
更改為__DIR__
因為參考您提到的錯誤消息( require_once(/QueryString.php)
), $sourcedir
是空的。
例如:如果SSI.php
和QueryString.php
在同一目錄中,請嘗試更改:
require_once(__DIR__ . '/QueryString.php');
與其他require
/ include
文件相同的東西
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.