【问题标题】:Include the default DirectoryIndex包括默认的 DirectoryIndex
【发布时间】:2013-11-05 06:15:09
【问题描述】:

我有DirectoryIndex index.php 并想include './'; 但我得到failed to open stream: Is a directory

我当然可以include substr($someString, -1)==='/'?$someString.'index.php':$someString;,但是有更简单的方法吗?可能是 .htaccess 或 php.ini 中的内容?

不就是答案。

【问题讨论】:

  • 我不明白您为什么要这样做,而不是简单地将 'index.php' 字符串添加到 './' 的末尾。 include './index.php'; 怎么了?

标签: php include directoryindex


【解决方案1】:

如果你想从当前目录包含你不需要任何路径:

include 'index.php';

如果你想从远程文件中包含你应该从 http 开始

include 'http://www.example.com/file.txt';

Attn : PHP 4.3.0 之前的 Windows 版本不支持通过此函数访问远程文件,即使启用了 allow_url_fopen 也是如此。

否则,如果您想从相对路径中包含您可以从'/... 开始,那么您可以从您的主机根目录或'../...... 从子目录开始。

【讨论】:

  • 您没有回答问题。具体目录未知。我想要一些能让目录变成索引文件的东西,比substr($someString, -1)==='/'?$someString.'index.php':$someString; 更容易,可能是 .htaccess 或 php.ini 中的东西。
【解决方案2】:

您不能包含目录。 PHP 只接受一个有效文件作为包含的参数,所以如果你想能够使用“./”或“somedir/”并让它在那个目录中找到 index.php 文件,你总是可以写一个以 dir 作为参数并返回索引文件的完整路径的函数。

类似:

function index($dir){
   return substr($dir, -1) === '/' ? $dir.'index.php' : $dir;
}

然后您可以调用include index("./"); 或其他一些目录名称并让它返回正确的字符串。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-04
    • 2014-03-13
    • 2014-08-03
    • 2011-12-17
    相关资源
    最近更新 更多