PHP/Windows - Opendir()无法打开符号链接目录中的子目录

PHP/Windows - Opendir()无法打开符号链接目录中的子目录

问题描述:

有谁知道这个问题的解决方案?我无法在符号链接的目录中打开子目录。我已经确认路径是正确的(甚至复制&将路径粘贴到资源管理器中,这可以很好地解析它)。这是一个奇怪的,令人讨厌的bug:|。PHP/Windows - Opendir()无法打开符号链接目录中的子目录

实施例:

C:\文件夹\ symbolic_link \ DIR1 \ DIR2 - 开口DIR2失败。

C:\文件夹\ symbolic_link \ DIR1 - 工程

C:\文件夹\ real_directory \ DIR1 \ DIR2 - 工程

C:\文件夹\ real_directory \ DIR1 - 工程

+0

它在资源管理器中工作,但是iis_user有权限查看它吗? – 2010-05-25 18:16:40

+0

@丹Thx!但我运行Apache(XAMPP LAMP堆栈),它似乎是一个PHP的opendir()的错误。我发现了一个可以追溯到2008年的PHP bug报告,这个报告在5.3中没有被修复:/。 – 2010-05-25 20:56:40

好吧,我终于找到了一个解决这个PHP在处理Windows上的符号链接的bug。使用opendir()递归迭代文件/目录时会发生该错误。如果当前目录中存在某个目录的符号链接,则opendir()将无法​​读取目录符号链接中的目录。这是由于php的statcache中有些东西造成的,可以通过调用clearstatcache()来解决,然后在目录符号链接上调用opendir()(同时父目录的文件句柄必须关闭)。

下面是修复的一个例子:

<?php 

class Filesystem 
{ 
    public static function files($path, $stats = FALSE) 
    { 
     clearstatcache(); 

     $ret = array(); 
     $handle = opendir($path); 
     $files = array(); 

     // Store files in directory, subdirectories can't be read until current handle is closed & statcache cleared. 
     while (FALSE !== ($file = readdir($handle))) 
     { 
      if ($file != '.' && $file != '..') 
      { 
       $files[] = $file; 
      } 
     } 

     // Handle _must_ be closed before statcache is cleared, cache from open handles won't be cleared! 
     closedir($handle); 

     foreach ($files as $file) 
     { 
     clearstatcache($path); 

      if (is_dir($path . '/' . $file)) 
      { 
       $dir_files = self::files($path . '/' . $file); 

       foreach ($dir_files as $dir_file) 
       { 
        $ret[] = $file . '/' . $dir_file; 
       } 
      } 
      else if (is_file($path . '/' . $file)) 
      { 
       $ret[] = $file; 
      } 
     } 

     return $ret; 
    } 
} 

var_dump(filessystem::files('c:\\some_path')); 

编辑:看来clearstatcache($path)前必须对symlink'd目录的任何文件处理函数被调用。 Php没有正确缓存符号链接的dirs。