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

在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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?