Question

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.

Was it helpful?

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.

OTHER TIPS

if you want to include from current directory you do not need any path :

include 'index.php';

else if you want to include from remote files you should start from http

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

Attn : Windows versions of PHP prior to PHP 4.3.0 do not support access of remote files via this function, even if allow_url_fopen is enabled.

else if you want to include from relative path you can start '/... then you start from your host root or '../...... for stating from sub directory .

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top