简体   繁体   中英

How to include a file that has includes (Including nested includes)

I am trying to get SSI to work for my SMF forums, but the way I'm doing it seems to be different than the intended method. I want to include the SSI.php file from a child of a different directory. 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 .

If I put include "/home/account/public_html/forums/SSI.php"; I get

require_once(/QueryString.php): failed to open stream: No such file or
directory in '/home/account/public_html/forums/SSI.php line 64'

I get that same result if I put it as a relative path.

I've tried chdir and $_SERVER["DOCUMENT_ROOT"] , with the same results.

Beginning of SSI.php (Where the error shows up) is:

<?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 is much simpler, consisting of this:

<?php 
include "/home/account/public_html/forums/SSI.php"; 
echo $sourcedir;
?>

Since this code is going to be used on a variety of different installs, not all of which allow editing of SSI.php , I'd love a way to make it work without having to edit SSI.php . Anything can be put in the file.php though.

Edit: Not a permissions issue. isreadable() returns true for all three files, and I can append to them without issue.

it seems you are use require / require_once inside file that already included using relative path, so the solution is using absolute path.

NB : __DIR__ constant will be a good way to define the absolute path of the file specialy if used inside includesee

So try to change $sourcedir to __DIR__ because refer to the error message you mentionned ( require_once(/QueryString.php) ), $sourcedir is empty.

for exemple : if SSI.php and QueryString.php are in the same directory, try to change this:

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

the same things to other require / include files

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM