dpndp64206 2014-02-24 05:23
浏览 287
已采纳

在glob函数路径中使用变量

As mentioned in PHP Manual

pattern

The pattern. No tilde expansion or parameter substitution is done.

I tried to find files with path name in a variable and it didn’t work.

The loader file (main function in the program) needs to include few files which reside in the same directory of the present class file that the loader file is working on . So , I tried below work around in the loader file.

$cwd = getcwd();
$reflector = new ReflectionClass($class_name); //$class_name  come from $_GET
$fname = $reflector->getFileName();
$fdir = dirname($fname);
chdir ($fdir);
$include_file_name = glob("*.inc");
chdir ($cwd);

It works but just wants to know is it a good work around or is there anything else I can do.

  • 写回答

1条回答 默认 最新

  • dongzu3511 2014-02-24 05:45
    关注

    If you have all of your classes inside one directory, you could just hard code it relative to the current script and that would eliminate the need for a reflection class. For example, if the current script is in /var/www/foo and the classes are in /var/www/classes, just do:

    $include_file_name = glob(__DIR__ . '/../classes' . '*.inc');
    

    However if the classes can come from multiple directories, using a reflector like that seems pretty appropriate to me, however you could also add $fdir into the glob() string instead of doing chdir() like this, but of course that's just micro-optimization:

    $include_file_name = glob($fdir ."/*.inc");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程