在 PHP 中使用通配符作为目录级别
在 PHP 中使用某种通配符表达式跳过目录级别的最佳方法是什么?
我有一个对于每个子目录都是唯一的 config.php 文件,我需要将其包含在页面的标题中。
示例:
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
我想要的是告诉我的 PHP 主页面在父目录的名称可能更改时包含 config.php?
类似于
What is the best way to skip over 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"); ?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您希望使用
glob
和 foreach 来包含每个匹配的文件。有关更多详细信息,请参阅 关于
glob
的手册页。You want to use
glob
and a foreach to include each file that matches.See the man page on
glob
for more specifics.如果位置由 dir1 和 dir2 定义,而第三个位置只是一个可读的未知数,那么您可以使用单个表达式来完成它:
注意:通过
http://mysite/.. 加载包含脚本。 .
是不可能的。 HTTP 不提供目录列表,并且它不会工作,因为在这种情况下 config.php 脚本将被解析掉。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:
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.