Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to create a Windows shortcut using PHP?

Tags:

php

windows

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.

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).

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?

like image 222
Drarok Avatar asked Apr 27 '09 10:04

Drarok


2 Answers

Thanks to the above answer, I found that you can indeed call COM from php, here's my first draft of a symlink() replacement:

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();
    }
}
like image 69
Drarok Avatar answered Sep 28 '22 22:09

Drarok


There is support for junction points (similar to UNIX symlinks) before Vista.

You need the linkd tool from the windows resource kit (free download).

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.

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()
like image 32
codeape Avatar answered Sep 29 '22 00:09

codeape