是否可以使用 PHP 创建 Windows 快捷方式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/792905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to create a Windows shortcut using PHP?
提问by Drarok
I'm writing a script to tidy up a bunch of media spread across my hard drives, and it works pretty well so far at home (on my Mac) as I use symlinks in a directory to give the impression that everything is organised in one location, whilst the actual data is spread across the 4 drives.
我正在编写一个脚本来整理散布在我的硬盘驱动器上的一堆媒体,到目前为止它在家里(在我的 Mac 上)运行得很好,因为我在目录中使用符号链接给人的印象是所有内容都组织在一个位置,而实际数据分布在 4 个驱动器上。
Unfortunately, I have to use Windows at work, and of course there's no symlink support there until PHP 5.3 (and I assume that requires Vista as that's when the command-line tool "mklink" first appeared).
不幸的是,我必须在工作中使用 Windows,当然在 PHP 5.3 之前没有符号链接支持(我假设这需要 Vista,因为那是命令行工具“mklink”第一次出现的时候)。
As a workaround, I considered creating a shortcut, but I can't find a way of doing it. Is it possible, or is there a better solution I've not considered?
作为一种解决方法,我考虑创建一个快捷方式,但我找不到这样做的方法。有没有可能,或者有没有我没有考虑过的更好的解决方案?
回答by Drarok
Thanks to the above answer, I found that you can indeed call COM from php, here's my first draft of a symlink() replacement:
感谢上面的回答,我发现你确实可以从 php 调用 COM,这是我的 symlink() 替换的初稿:
if (! function_exists('symlink')) {
function symlink($target, $link) {
if (! substr($link, -4, '.lnk'))
$link .= '.lnk';
$shell = new COM('WScript.Shell');
$shortcut = $shell->createshortcut($link);
$shortcut->targetpath = $target;
$shortcut->save();
}
}
回答by codeape
There is support for junction points (similar to UNIX symlinks) before Vista.
在 Vista 之前支持连接点(类似于 UNIX 符号链接)。
You need the linkd
tool from the windows resource kit (free download).
您需要linkd
Windows 资源工具包中的工具(免费下载)。
Shortcuts are just files. You can create shortcut files using the COM WScript API. The sample code does this using Python. If there exists a library for PHP that lets you do COM interop, you should be able to do something similar.
快捷方式只是文件。您可以使用 COM WScript API 创建快捷方式文件。示例代码使用 Python 执行此操作。如果存在允许您执行 COM 互操作的 PHP 库,您应该能够执行类似的操作。
import win32com.client
import winshell
userDesktop = winshell.desktop()
shell = win32com.client.Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(userDesktop + '\Zimbra Webmail.lnk')
shortcut.Targetpath = r'C:\Program Files\Mozilla Firefox\firefox.exe'
shortcut.Arguments = 'http://mysite.com/auth/preauth.php'
shortcut.WorkingDirectory = r'C:\Program Files\Mozilla Firefox'
shortcut.save()
回答by DaveRandom
For the record, junction points on NTFS are indeed only for directories. I have had great success using linkd.exe to create a virtual filesystem, but there are a few things to be aware of:
为了记录,NTFS 上的连接点确实仅用于目录。我使用 linkd.exe 创建虚拟文件系统取得了巨大的成功,但有几点需要注意:
- You cannot link between volumes (i.e. trying to create a link in C: that points to a location in D: won't work)
- Unlinking things seems to sporadically delete data at the destination. Backup/Move you data before you remove a link!
- Junction points suffer the same problems when sharing data over the network as mounted volumes - sometimes they simply don't work remotely. They also seem to randomly (i.e. sometimes) inherit some of the share permissions from the source folder, no matter how you configure the destination link.
- 您无法在卷之间进行链接(即尝试在 C: 中创建指向 D: 中某个位置的链接将不起作用)
- 取消链接似乎偶尔会删除目的地的数据。在删除链接之前备份/移动您的数据!
- 在通过网络共享数据作为挂载卷时,连接点会遇到同样的问题——有时它们根本无法远程工作。无论您如何配置目标链接,它们似乎也随机(即有时)从源文件夹继承一些共享权限。
Cheers for the useful posts about creating .lnk files, just what I was after...
为有关创建 .lnk 文件的有用帖子干杯,这正是我所追求的......