douniang3866
2013-05-09 23:22
浏览 56
已采纳

PHP lstat命令不区分Windows中的快捷方式

In windows, I open a dir, read the files, and for each file, run stat to determine the size, etc.

The problem is that when I run stat on a folder SHORTCUT, it comes back as a FOLDER, and I can't see anywhere in the mode bitmask that might indicate this. This has been true for all of the folder shortcuts in c:\Documents and Settings\myUserName\.

For these shortcuts, is_file returns false, is_dir returns true and is_link isn't supported in XP.

Here's an excerpt from my code (it has been trimmed down, so there may be bugs) :

if(($h=@opendir($root))!==false){
    while (false !== ($file = readdir($h))){
        if(!($file=="." || $file=="..")){
            if( $stat = @lstat($root . $file) ){
                $ary[0] = $file;
                $ary[1] = $root;
                $ary[2] = Date("m/d/y H:i:s", $stat['mtime']);
                if($stat['mode'] & 040000){
                    $ary[3]="dir";
                    $ary[4]=0;
                }else{
                    $ary[3] ="file";
                    $ary[4] = $stat['size'];
                }
                echo(json_encode($ary));
            }
        }
    }
}

A workaround for this will be appreciated...

EDIT: Winterblood's solution almost worked

First off - my bad - it's a win7 machine.

Thanks Winterblood for the quick turnaround - this worked for several of the shortcuts, and the PHP manual says just that... However,

c:\users\myUserName\AppData\Local\Application Data

(and others) are still coming back as directories, while winSCP correctly sees them as shortcuts. As a matter of fact, the 'mode' is 040777, which is exactly the same as many real folders.

Any other suggestions?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • duan0818 2013-05-09 23:30
    最佳回答

    PHP's stat() function "follows" shortcuts/symlinks, reporting details on the linked file/folder, not the actual link itself.

    For getting stat details on the link itself use lstat().

    More information in the PHP documentation on lstat.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题