Php – get the short DOS name of a file from PHP

dosPHP

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?

Best Answer

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