We are using php to call a batch file that launches a couple of applications.

We use:
pclose(popen( "start /B $command, "r" )) ;
where
command = c:\path\to\file\batchfile.bat

One of the applications has a dll that is required to function properly. The dll is in the same folder as the bat files which is also where the exe files are that are being launched.

In the bat file, the first thing we do is:

chdir C:\path\to\file\

and when we call start on the exe, we use:

start /d "C:\path\to\file\" program.exe

If I run the bat file from any random directory using console it runs fine.

If I run the bat file by double clicking it of course it runs fine.

But, if we run it using PHP it seems to not allow access to the support dll for some reason. The api we are using gives us error code -1.

I tried a quick test and renamed the dll and reran the cases that typically work(listed above). It then gave error code -7.

So, based on that, I think it knows where the dll is(based on different error code) but it's as if windows won't let it use/access the dll when initiated through php.

Has anyone seen something similar to this?

I also tried making a shortcut with "run as administrator" selected and called the shortcut from php. It did not help.

Thanks,
Ryan

    6 days later

    When you say "run it using PHP" I assume you mean that you've written a PHP script that attempts to run the bat file. Perhaps you should post this PHP code? Also, are you trying to execute this PHP script from the command line or are you trying to execute the PHP script via apache or some web server?

      5 days later

      The PHP script to call the BAT file is executed from a command-line shell command that is called from another PHP script that is called from a web-server.

      script1.php (Called from web server):

      $command = "php script2.php" ;
      pclose(popen( "start /B $command));

      script2.php:

      $command = "c:\path\to\file\batchfile.bat" ;
      pclose(popen( "start /B $command, "r" )) ;

      The reason for the two scripts is that script2.php needs to be initiated from the web server but run independently on its own thread.

      I have no experience with popen/pclose. I fail to see how two scripts results in a separate thread being created. I'm not saying it doesn't, it just seems to me that the docs on [man]popen[/man] suggest that a command will be forked:

      Opens a pipe to a process executed by forking the command given by command

      .

      I know from a prior thread of mine that it is possible to concoct a command for a *nix machine that will fork off a background process. This results in the exec command returning immediately and the process continuing as a background process. The most helpful code is on the last post of that thread.

      That was pretty old-school, though, and specific to *nix machines. I have also had great success using [man]pcntl_fork[/man] but according to the docs, the PCNTL extension is not available on windows platforms.

      I've barely ever used the command line on a windows machine but some searching suggested that maybe you try using cmd:

      system("cmd /c C:[path to file]");

      I also saw this:

      The START command optionally accepts a title for the created window as its first argument

      Because your command has a space in it, it might be interpreting 'php' as the title and script2.php as the command -- which may not work. Try enclosing in quotes?

      I'd also point out that you might try running your PHP command from the command line -- then your script would run with your user's permissions. If your PHP script is run via a web server then it will be run using the permissions of the web server user -- which may not be permitted to run shell commands depending configuration. You might try executing a trivial command (dir, for instance) to see if PHP can even execute a shell command.

      this may also be a permissions issue.

        We did more testing and the scripts are working fine when called from a command prompt i.e. (C:\Program Files (x86)\PHP\php.exe -f script1.php)

        Everything launches as we would expect.

        The issue seems to be introduced when the script is being called from a server. It's somewhat difficult to debug because the exe we are launching utilizes a 3rd party plugin (.dll) and we don't have a way to see where it is failing. It does return error codes but in this case it returns a -1 which is a generic error code. This is what leads me to believe it may be some restriction on applications (or their functionality) that are launched from a server.

        I may just try using some other php calls to launch and see if they behave differently.

          you should also check php info:

          phpinfo();
          

          It may tell you something about command line execution restrictions. I also just looked in the docs:
          http://php.net/manual/en/intro.exec.php
          And see that it says

          All execution functions call the commands through cmd.exe under Windows. Therefore the user calling these functions needs appropriate privilege to run this command. The only exception is proc_open() with bypass_shell option.

          Not sure if that helps...

            I have run into a similar issue in the past (that I was unable to solve) where I was trying to launch VLC and play a video; however, it just would not work when invoked via the server/webpage, but would run exactly as expected when run from the command line. I did some digging and if I remember correctly it had something to do with the permissions of the Apache service. If my aging memory serves... I believe there is a setting that can be set on the service that has something to do with interacting with the desktop and I think that needs to be checked. When I did that I was able to get VLC to launch and play the file, but I was unable to get video (audio only). It was at that point I decided to stop and just use Kodi instead, and therefore stopped trying to get it to work.

              3 years later

              rlogan
              Hi rlogan
              I have the same problem to launch executable from php. Were you find a solution to the problem?
              I searched many many resources on the network but nothing was good for me.

                Write a Reply...