Php 如何添加到路径“index.html”

Php 如何添加到路径“index.html”,php,Php,如果路径尚未选择文件,如何向路径添加索引?我的意思是,如果用户进入/this/page,它会将其更改为/this/page/index.html,如果index.php不存在,则会将其更改为/this/page/index.php。如果index.php和index.html都不存在,我希望它回显错误 以下是我尝试过的: if(!file_exists($filename)) { if(strpos($filename, ".") === false){

如果路径尚未选择文件,如何向路径添加索引?我的意思是,如果用户进入/this/page,它会将其更改为/this/page/index.html,如果index.php不存在,则会将其更改为/this/page/index.php。如果index.php和index.html都不存在,我希望它回显错误

以下是我尝试过的:

        if(!file_exists($filename)) {
            if(strpos($filename, ".") === false){
                $nfilename = $filename . "index.html";
                if(!file_exists($nfilename)){
                    $filename .= "index.php";
                    if(!file_exists($filename)){
                        echo 'error';
                    }
                } else $filename = $nfilename;
            } else {
                echo 'error';
            }
        }

您可以使用以下代码检查请求uri是否具有index.php或index.html:

if (strpos($_SERVER['REQUEST_URI'], 'index.php') === false
    || strpos($_SERVER['REQUEST_URI'], 'index.html') === false) {
    // redirect to $_SERVER['REQUEST_URI'] plus one of the index
}

查看阵列以了解更多信息

您的最终目标是什么?@amphetamachine,因为它已被重定向。它显示一个检查cookies的PHP页面,如果用户允许,它会通过从路径获取数据来显示页面内容。@amphetamachine PHP文件需要在给定路径中显示文件内容。但是,当给定的路径不是以文件结尾,而是以文件夹结尾时,它找不到该文件。您确定需要PHP吗?从…/dir/重定向到…/dir/index.php通常由Apache处理。只需将$\u服务器['REQUEST\u URI']更改为包含要检查的路径的变量,它对我有何帮助?如果路径以post.php结尾怎么办?它将返回true,就像它不会以任何文件结束一样。