Search code examples
phpincludedirectoryindex

Include the default DirectoryIndex


I have DirectoryIndex index.php and want to include './'; But I get failed to open stream: Is a directory

Of course I can include substr($someString, -1)==='/'?$someString.'index.php':$someString;, but is there an easier way? Maybe something in .htaccess or php.ini?

No is an answer.


Solution

  • You can't include a directory. PHP will only accept a valid file as an argument for include, so if you want to be able to use "./", or "somedir/" and have it find the index.php file in that dir, you could always write a function which takes the dir as an argument and returns a full path to the index file within.

    Something like:

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

    Then you could call include index("./"); or some other directory name and have it return the proper string.