PHP system call always fails

(Apparently) no matter what command I try to run with a PHP function system

, the command always does nothing and then exits with exit code 1. For example,

system("echo 'this should definitely work'", $retval);

      

installs $retval = 1

. Thus,

system("dir", $retval);

      

as well as launching the executable that I wrote; when i run

vfmt -h cat.v

      

from cmd.exe

command works and returns with exit code 0, but works

system("vfmt -h cat.v", $retval);

      

installs again $retval = 1

. This file vfmt.exe

is located in the same directory as the src.php

script that is trying to make these calls system

.

I'm almost out of my mind trying to figure out what happened. What could be causing this problem?

+3


source to share


4 answers


You should check yours php.ini

for the line like this:

disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
                   ^^^^          ^^^^^^^^^^^^^^^^^^

      

etc.



Also check the status of "safe mode" (php ver. <5.4), if you enabled it, you can only execute files in safe_mode_exec_dir

etc ...

More information in the document and for command execution here and especially for the system here .

+2


source


echo

is always an internal shell command, not a separate executable file. You need something more like



system('/bin/sh -c "echo \'foo\'"'); // unix-ish
system('cmd.exe /c echo foo'); // windoze

      

+1


source


Most likely the problem is that the "working directory" is not what you expect.

This is hidden because you are checking the return value and not the screen output; they are different.

First, for monitoring: try using exec

http://au2.php.net/manual/en/function.exec.php as it will help you debug by providing both screen output and return value.

Second, to fix:

1 - Specify full paths to both executable and included files. Assuming it is Windows (you refer to cmd.exe), run

system("c:\PathToPhpFile\vfmt -h c:\PathToCatVFile\cat.v, $retval);

      

or

2 - Replace the working directory before calling system()

or exec()

using chdir()

first.

(Or you can cd in a batch file (windows) or concatenate commands in Linux, but this is less portable.)

+1


source


You need to redirect the output of the command you are using with system () so that it runs in the background. Otherwise PHP will wait for the program to complete before continuing.

system('mycommand.exe > output.log', $retval);
system('mycommand.exe 2>&1', $retval);

      

Not sure how you are calling your script, but perhaps you are using PHP's max_execution_time (or related) limit. Something to explore. Also, since you are working with windows, you may need to use absolute paths with your commands when making system calls, since the file you are calling cannot be in any specific PATH.

+1


source







All Articles