2
votes

Is there any way to recognize if a folder is a junction point in PHP on a windows system? Or even get the path / name of the target?

is_link or realpath do not support it.

Thanks!

1
If you know the shell command you have at least a workaround. - Álvaro González
Yes, thought of using some command, but don't know one yet. - lsblsb

1 Answers

0
votes

Yes, by using lstat(), see http://php.net/manual/de/function.lstat.php

It returns an array with pieces of information about the filename (dir/junction). You need compare the key mode against a certain bitmask.

mode equals st_mode in the underlying OS layer. The _stat struct is defined in SYS\STAT.H. It includes a field st_mode.

About the st_mode bitmask:

Bit mask for file-mode information. The _S_IFDIR bit is set if path specifies a directory; the _S_IFREG bit is set if path specifies an ordinary file or a device. User read/write bits are set according to the file's permission mode; user execute bits are set according to the filename extension.

     * #define  _S_IFDIR    0x4000
     * #define  _S_IFREG    0x8000

Quoted from: https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/stat-functions?view=vs-2017


PHP Implementation for isJunction(): https://github.com/composer/composer/blob/master/src/Composer/Util/Filesystem.php#L654-L678

Warning This is very fragile after the lastest changes to Windows. See https://www.bountysource.com/issues/53997655-composer-update-is-deleting-pathed-windows-symlinked-local-repos-again-ref-4955