PHP:如何确定文件是否是 Windows 平台上的快捷方式?

发布于 2024-10-31 05:42:25 字数 669 浏览 3 评论 0原文

排除尚未与 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 使用 finfomime_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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姜生凉生 2024-11-07 05:42:25

就像填写答案一样。几年前写的,不确定它是否适用于当前的 Windows 版本。

要检查魔术字节,只需使用:

 $bin = file_get_contents("file.lnk", 2048);
 if (substr($bin, 0, 20) == "L\000\000\000\001\024\002\000\000\000\000\000\300\000\000\000\000\000\000F") {

如果您想提取路径:

  function decode_windows_visual_shortcut($bin) {

            # taken from "The Windows Shortcut File Format.pdf" V1.0 as
            # reverse-engineered by Jesse Hager <[email protected]> 

            if (!defined("WIN_LNK_F_ITEMLIST")) {

                    define("WIN_LNK_F_ITEMLIST", 1);
                    define("WIN_LNK_F_FILE", 2);
                    define("WIN_LNK_F_DESC", 4);
                    define("WIN_LNK_F_RELATIVE", 8);
                    define("WIN_LNK_F_WORKDIR", 16);
                    define("WIN_LNK_F_CMDARGS", 32);
                    define("WIN_LNK_F_ICON", 64);   
                    define("WIN_LNK_F2_DIR", 16);   

                    function bread(&$bin, &$p, $bytes=4) {
                            $h = bin2hex( strrev($s = substr($bin, $p, $bytes)) );
                            $v = base_convert($h, 16, 10);
                            $p += $bytes;
                            return($v);  
                    }
            }

            $res = array();
            $p = 0x14;
            $fl=$res["flags"] = bread($bin,$p);
            $res["t_attr"] = bread($bin,$p);   
            $p = 0x4C;

            if ($fl & WIN_LNK_F_ITEMLIST) {
                    #-- don't need this
                    $p += bread($bin,$p,2);
            }

            if ($fl & WIN_LNK_F_FILE) {
                    #-- File Location Info
                    $p0 = $p;
                    $p = $p0 + 0x10;
                    $p_path = $p0 + bread($bin,$p);
                    $p = $p0 + 0x18;
                    $p_file = $p0 + bread($bin,$p);
                    $path = substr($bin, $p_path, 704);
                    $path = substr($path, 0, strpos($path, "\000"));
                    $file = substr($bin, $p_file, 704);
                    $file = substr($file, 0, strpos($file, "\000"));
                    $res["path"] = $path;
                    $res["file"] = $file;
            }

            return($res);
    }

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:

 $bin = file_get_contents("file.lnk", 2048);
 if (substr($bin, 0, 20) == "L\000\000\000\001\024\002\000\000\000\000\000\300\000\000\000\000\000\000F") {

And if you want to extract the path(s):

  function decode_windows_visual_shortcut($bin) {

            # taken from "The Windows Shortcut File Format.pdf" V1.0 as
            # reverse-engineered by Jesse Hager <[email protected]> 

            if (!defined("WIN_LNK_F_ITEMLIST")) {

                    define("WIN_LNK_F_ITEMLIST", 1);
                    define("WIN_LNK_F_FILE", 2);
                    define("WIN_LNK_F_DESC", 4);
                    define("WIN_LNK_F_RELATIVE", 8);
                    define("WIN_LNK_F_WORKDIR", 16);
                    define("WIN_LNK_F_CMDARGS", 32);
                    define("WIN_LNK_F_ICON", 64);   
                    define("WIN_LNK_F2_DIR", 16);   

                    function bread(&$bin, &$p, $bytes=4) {
                            $h = bin2hex( strrev($s = substr($bin, $p, $bytes)) );
                            $v = base_convert($h, 16, 10);
                            $p += $bytes;
                            return($v);  
                    }
            }

            $res = array();
            $p = 0x14;
            $fl=$res["flags"] = bread($bin,$p);
            $res["t_attr"] = bread($bin,$p);   
            $p = 0x4C;

            if ($fl & WIN_LNK_F_ITEMLIST) {
                    #-- don't need this
                    $p += bread($bin,$p,2);
            }

            if ($fl & WIN_LNK_F_FILE) {
                    #-- File Location Info
                    $p0 = $p;
                    $p = $p0 + 0x10;
                    $p_path = $p0 + bread($bin,$p);
                    $p = $p0 + 0x18;
                    $p_file = $p0 + bread($bin,$p);
                    $path = substr($bin, $p_path, 704);
                    $path = substr($path, 0, strpos($path, "\000"));
                    $file = substr($bin, $p_file, 704);
                    $file = substr($file, 0, strpos($file, "\000"));
                    $res["path"] = $path;
                    $res["file"] = $file;
            }

            return($res);
    }

http://code.google.com/p/8bits/downloads/detail?name=The_Windows_Shortcut_File_Format.pdf

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文