【问题标题】:How to include a file that has includes (Including nested includes)如何包含包含的文件(包括嵌套包含)
【发布时间】:2020-12-04 22:10:21
【问题描述】:

我正在尝试让 SSI 为我的 SMF 论坛工作,但我这样做的方式似乎与预期的方法不同。我想include SSI.php 来自不同目录的子目录的文件。换句话说,SSI.php 文件位于/home/account/public_html/forums/SSI.php,而include SSI.php 文件位于/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,我可以毫无问题地附加到它们。

【问题讨论】:

  • 我认为是权限原因,请尝试更改文件及其父项的权限/所有者
  • @GNassro 不,不是权限原因。 isreadable() 返回真。我会把它加在上面。每个单独的文件都可以编辑。根据我的阅读,PHP 使用调用文件来引用任何相关链接,我看不到在调用文件中更改它的方法。

标签: php ssi mantis smf-forum


【解决方案1】:

看来您在已经包含使用相对路径的文件中使用require/require_once,因此解决方案是使用绝对路径。

NB:如果在include see 中使用,__DIR__ 常量将是定义文件绝对路径的好方法。

所以尝试将$sourcedir更改为__DIR__,因为参考您提到的错误消息(require_once(/QueryString.php)),$sourcedir为空。

例如:如果SSI.phpQueryString.php在同一个目录下,尝试改变这个:

require_once(__DIR__ . '/QueryString.php');

与其他require/include 文件相同

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-13
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多