tags:

views:

164

answers:

4

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?

A: 

How about backslashing the space?

Home/My Documents/  --> Home/My\ Documents/
Ian Elliott
The executable I'm trying to run treats the backslash as part of the name. In your example it would try to open the file Home/My\
Don Kirkby
You probably want escapeshellarg as suggested by phphil. You would escape with a backslash if calling directly from the command line
David Caunt
+1  A: 

You may want to have a look at escapeshellarg() and put the parameter in between double quotes.

Philippe Gerber
I thought that should work, too. It works when I execute the dir command, but not when I execute the command I'm trying to run.
Don Kirkby
use escapeshellcmd() to escape the executable, and escapeshellarg() for the arguments.
Philippe Gerber
Thanks for the suggestion, but I don't understand escapeshellcmd at all. On Windows, it just appears to replace special characters with spaces. 'C:\Documents and Settings\don\foo' becomes 'C: Documents and Settings don foo'. How is that useful?
Don Kirkby
+1  A: 

You want the GetShortPathName API in Kernel32 on windows. To call this from PHP, you will need to use the Win32 API extension...

That would probably be faster. Unfortunately I can't test it because the php 5.3rc2 build came without the w32 module :-/ Hope you don't mind me adding the links to your post.
VolkerK
+3  A: 

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
VolkerK