Hello everyone,
I'm having a problem with exec() on a Windows server. I've read posts all around the web but nothing has helped. Safe mode is off on php.

I'm trying to call an exe in my Program Files. It works in the command prompt window but not through php. I've tried everything.

Here is my code:

$output = array();
$return_var = null;
$cmd =' "C:\Program Files\NCH Swift Sound\Switch\switch.exe"  ';
exec($cmd, $output, $return);
print $return;
print $output;

This outputs: 0Array
and the Apache log gives the error 'C:\Program' is not recognized as an internal or external command etc.

I've tried varoius combinations of " " and \ to no effect. I may need to set up cmd.exe for IUSER but I'm not sure what this is.

Any help greatly appreciated - as I'm at a dead end,
Thanks
Mark

    Thanks for your reply

    Yes, I've tried system, passthru and exec on simple examples just to see if I was
    getting through to cmd.exe.

    Like this:

    $last_line = system('dir', $return);

    Which works giving a normal 'dir' output.

    The problem is when I try to run an .exe - it would seem to be with the format of my command statement in php. I've tried all sorts of combinations:

    $cmd = '"c:\\Program Files\\NCH Swift Sound\\Switch\\switch.exe\\"';

    but nothing works!!!

    Does anyone know if this could be a permissions problem or just my lousy formatting? It's driving me mad - I've spent over 9 hours on this simple problem.

      are you trying to launch a exe file or run commands to a exe file from the command prompt? Have you tried outputting it into a textarea box?

        Hi again,

        I'm trying to launch an exe file and pass it arguments.

        I've managed to get to launch the exe now.

        The problem now is that I can't pass the arguments in.
        My aim is to run something like this:

        $cmd="\"c:\\Program Files\\NCH Swift Sound\\Switch\\switch.exe\"  -convert  \"c:\\mms2\\bells.amr\"  -format MP3";

        but even though it runs in command prompt - there's a problem from php with the second batch of inverted commas.

        Any ideas greatly appreciated - this is the final stumbling block.

        Thanks again!
        Mark

        *** ps for anyone interested or in a similar situation the original problem of php
        not running an exe was solved by going to services.msc and right clicking on Apache2 and selecting 'Allow service to interact with desktop'. I'm running Apache 2 on Windows NT

          Hello everyone,

          Just to let anyone interested know how I got around my problem. I managed to find
          information around different forums from previous posts that helped out.

          To repeat, I'm running php with Apache 2 on Windows NT. I've been trying to call
          an .exe from php.

          I've modified this function by a guy called Jonah - posted at php.net:

          <?php
          function execInBackground($path, $exe, $additional) {
            global $conf;
          
          if (file_exists($path . $exe)) {
            chdir($path);
            if (substr(php_uname(), 0, 7) == "Windows"){
            echo 'going Windows';
                pclose(popen("start \"bla\" \"" . $exe . "\" "  . $additional, "r"));           } else {
                exec("./" . $exe . " " .  $additional . " > /dev/null &");           }
            }
          }
          
          //-convert "C:\\mms2\\bells.amr" -format MP3 -outfolder "C:\\mms2"
          execInBackground("C:\\Program Files\\NCH Swift Sound\\Switch\\", "switch.exe",  '-convert c:\\mms2\\bells.amr - format MP3');
          ?>
          

          I'm calling this with my arguments:

          execInBackground("C:\\Program Files\\NCH Swift Sound\\Switch\\", "switch.exe",  ' c:\\mms2\\bells.amr - format MP3');

          And everything is thankfully working fine. Th method could be prettier but it works!

          The earlier problem I had - see above, was related to setting permissions for local users to have access to the desktop.

          Hope this helps someone,
          Thanks
          Mark

            Write a Reply...