Use a wildcard as a directory level in PHP
What is the best way to skip ov开发者_StackOverflower a directory level in PHP using some kind of wildcard expression?
I have a config.php file that is unique to each child directory and I need to include it in the header of my page.
Example:
http://mysite.com/dir1/dir2/could-be-anything/config.php
http://mysite.com/dir1/dir2/something-different/config.php
http://mysite.com/dir1/dir2/different-again/config.php
What I want is to tell my main PHP page to include config.php when the name of it's parent directory may change?
Something like <?php include("http://mysite.com/dir1/dir2/*/config.php"); ?>
You want to use glob
and a foreach to include each file that matches.
foreach (glob("path/*/config.php") as $config)
include($config);
See the man page on glob
for more specifics.
If the location is defined by dir1 and dir2 and the third one is simply a readable unkown, then you can accomplish it with a single expression:
include(current(glob("./dir1/dir2/*/config.php")));
Note: Loading an include script over http://mysite/...
is not possible. HTTP provides no directory listings, and it wouldn't work because that config.php script would be parsed away in that case.
精彩评论