PHP:如何确定文件是否是 Windows 平台上的快捷方式?
排除尚未与 PHP 捆绑的任何第 3 方扩展,是否有任何方法可以确定 Windows 计算机上的给定文件是否是快捷方式/链接?内置函数 is_link
仅适用于 *nix 平台,因此在 Windows 计算机上运行的以下命令不会返回预期的结果:
$filePath = 'C:\somefile.lnk'; // path to shortcut file
var_dump(is_file($filePath)); // returns true
var_dump(is_link($filePath)); // returns false
这会在尝试使用目标文件时导致问题快捷方式,并最终对快捷方式文件本身进行操作。快捷方式的前四个字节通常是 4c 00 00 00
。但这个起始字节序列总是仅限于快捷方式似乎并不可靠。
附带问题:如何从快捷方式文件中提取目标路径? (注意:readlink()
似乎返回快捷方式文件本身的路径)。
编辑:只是为了省点麻烦,根据 PHP 使用 finfo
或 mime_content_type
的快捷方式文件的 mime 类型是“application/octet-stream ”,这确实没有帮助。
Excluding any 3rd party extension not already bundled with PHP, Is there any method to determine if a given file on a Windows machine is a shortcut/link? The built-in function is_link
works only on *nix platforms, so the following ran on a Windows machine will not return what might be expected:
$filePath = 'C:\somefile.lnk'; // path to shortcut file
var_dump(is_file($filePath)); // returns true
var_dump(is_link($filePath)); // returns false
This would cause problems when trying to work with the target file of the shortcut, and wind up operating on the shortcut file itself. The first four bytes of a shortcut seems to be typically 4c 00 00 00
. But it doesn't seem reliable that this opening byte sequence is always limited to shortcuts.
Side question: how do you extract the target path from a shortcut file? (Note: readlink()
seems to return the path of the shortcut file itself).
Edit: just to save some trouble, the mime-type of shortcut files according to PHP using finfo
or mime_content_type
is "application/octet-stream", which really won't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
就像填写答案一样。几年前写的,不确定它是否适用于当前的 Windows 版本。
要检查魔术字节,只需使用:
如果您想提取路径:
http://code.google.com/p/8bits/downloads/detail?name=The_Windows_Shortcut_File_Format.pdf
Just as fill answer. Wrote that years ago, not sure if it works with current Windows versions.
For checking the magic bytes just use:
And if you want to extract the path(s):
http://code.google.com/p/8bits/downloads/detail?name=The_Windows_Shortcut_File_Format.pdf