包括父文件夹 PHP 中的文件



在我的根html目录中,我安装了SMF,也安装了CodeIgniter。 我正在制作一个用于 CI 的 SMF SSI 自定义库,但我在包含 SSI.php 文件时遇到问题。 这是我的 html 根目录的样子:

- html/root
  /forums (html/forums)
      -SSI.php
  /application
      /libraries
          -SMF.php(html/application/libraries)
  /system

那么在文件应用程序/库/smf.php中,我将如何包含SSI.php? 这是我正在使用的,但它说找不到文件

include('.../forums/SSI.php');

当您的文件放入html/application/libraries文件夹中并且要包含的文件已放入html/forums文件夹中时,您可以使用以下代码来包含。

../../forums/SSI.php

要获取基目录,请使用以下选项之一:

  $_SERVER['SERVER_NAME'];

或:

  $_SERVER['DOCUMENT_ROOT'];
我不知道

这在库中是否可用,但 FCPATH 通常指向您的 CI 根目录,而 APPPATH 通常指向您的应用程序目录。希望有所帮助,这些设置在 CI 根索引中.php您可以尝试将它们回显出来,看看它们是否进入了库,如果不在其中添加一个 setter,就像

/**
 * Storage holder for the include files paths, is changed by $this->include_file()
 *
 * @access protected
 * 
 * @var string
 */
protected $_path;
/**
 * Sets the path for include files.
 *
 * @access private
 * 
 * @param string $path Path of files to include.
 */
private function set_path($path)
{
  $this->_path = $path;
}
// ------------------------------------------------------------------------
/**
 * Includes file.
 * 
 * @param  string $file Filename to include
 * 
 * @return void
 */
function include_file($file)
{
  include($this->_path . $file);
}

然后从 CI 范围设置它

<?php
  $this->library->set_path(FCPATH);
  $this->library->include_file('SI.php');

如果我正确理解您的查找内容,这样的事情可能会起作用。

这是两个时期,而不是一个。一个表示当前目录,两个表示父目录,任何其他表示 true 目录:

require_once('../{URL}');

你最好使用绝对路径。

include __DIR__ . '/../../forums/SSI.php';                             // php version >= 5.3 
include dirname(__FILE__) . '/../../forums/SSI.php'              //  php version < 5.3

使用

include_once FCPATH."forums/SSI.php";

FCPATH 是 CodeIgniter Installation 的根文件夹

最新更新