views:

22

answers:

0

I am trying to an execute a program in PHP on Windows with multiple arguments and absolute paths using a method like this:

<?php
$FLACPATH = str_replace(" ","\ ",realpath("../../encoders/flac.exe"));
$LAMEPATH = realpath("../../encoders/lame.exe");
$FILEPATH = realpath("../../encoders/01.flac");

function my_exec($cmd, $input='')
 {
    echo $cmd."<br/><br/>"; 
    $proc=proc_open($cmd, array(0=>array('pipe', 'r'), 1=>array('pipe', 'w'), 2=>array('pipe', 'w')), $pipes);
    fwrite($pipes[0], $input);fclose($pipes[0]);
    $stdout=stream_get_contents($pipes[1]);fclose($pipes[1]);
    $stderr=stream_get_contents($pipes[2]);fclose($pipes[2]);
    $rtn=proc_close($proc);
    return array('stdout'=>$stdout,
               'stderr'=>$stderr,
               'return'=>$rtn
              );
 }

var_export(my_exec("\"$FLACPATH\" -t \"$FILEPATH\""));
?>

In a Windows command prompt, this call works perfectly and looks like this: "C:\Users\James\Documents\My Dropbox\Code\Program\bin\Debug\encoders\flac.exe" -t "C:\Users\James\Documents\My Dropbox\Code\Program\bin\Debug\encoders\01.flac"

This works fine in PHP when I am using one absolute path, for instance if I am just executing the program. However, if I add another absolute path to my arguments to target a resource, the call fails. From what I can tell, it looks like PHP removes the quotes around my original call to the EXE, making the spaces break the call.

I am using absolute paths because I do not want to have my EXE files in the same directory as my PHP script. I use realpath($str) because I need the paths to be somewhat relative.

Does anyone know why PHP is stripping my quotes out of my exec()??? Thanks!