Can I get the short DOS name of a file from PHP?
Question
I want to pass a file path as a parameter to an executable from PHP, and the file path may contain spaces. The executable doesn't seem to handle quotes around the parameter, so I thought maybe I could pass the short DOS name instead of the long name.
Does PHP know anything about the old-style DOS 8.3 file names?
Solution
php/win32 ships with the COM/.net extension built-in. You can use it to create a WSH FileSystemObject and then query the ShortPath property of the File object.
<?php
$objFSO = new COM("Scripting.FileSystemObject");
$objFile = $objFSO->GetFile(FILE);
echo "path: ", $objFile->Path, "\nshort path: ", $objFile->ShortPath;
prints e.g.path: C:\Dokumente und Einstellungen\Volker\Desktop\test.php short path: C:\DOKUME~1\Volker\Desktop\test.php
OTHER TIPS
You may want to have a look at escapeshellarg()
and put the parameter in between double quotes.
You want the GetShortPathName API in Kernel32 on windows. To call this from PHP, you will need to use the Win32 API extension...
How about backslashing the space?
Home/My Documents/ --> Home/My\ Documents/