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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 socket通信实现多人聊天室疑惑
  • ¥15 DEV-C++编译缺失
  • ¥33 找熟练码农写段Pyhthon程序
  • ¥100 怎么让数据库字段自动更新
  • ¥15 antv g6 力导向图布局
  • ¥15 quartz框架,No record found for selection of Trigger with key
  • ¥15 锅炉建模+优化算法,遗传算法优化锅炉燃烧模型,ls-svm会搞,后面的智能算法不会
  • ¥20 MATLAB多目标优化问题求解
  • ¥15 windows2003服务器按你VPN教程设置后,本地win10如何连接?
  • ¥15 求一阶微分方程的幂级数