Question

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?

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top