3

(Seemingly) no matter what command I try to run with PHP's system function, the command always does nothing and then fails with exit code 1. For example,

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

sets $retval = 1. So does

system("dir", $retval);

as well as running an executable that I've written; when I run

vfmt -h cat.v

from cmd.exe the command works and returns with exit code 0, but running

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

again sets $retval = 1. This vfmt.exe file is in the same directory as the src.php script that is attempting these system calls.

I am nearly at my wit's end trying to figure out what's wrong. What could possibly be causing this issue?

6
  • 2
    You should incluclude to the question informations about your environment, such: what server, how do you run php (e.g. server-loaded or php-cgi) and such.. And also some lines from the error-log-file ... Othervise, all answer would only "guessing" - such mine bellow. Commented Aug 24, 2014 at 10:11
  • Add 2>&1 to the end of your command to redirect errors from stderr to stdout. This should make it clear what's going wrong. see stackoverflow.com/questions/538939/… Commented Aug 27, 2014 at 17:50
  • 2
    Try to add path to vfmt.exe, for example: system("c:\\path_to_exe\\vfmt.exe -h cat.v", $retval); Commented Aug 28, 2014 at 14:55
  • Are you missing a close quote in the original script, or just the question as posed? Commented Aug 29, 2014 at 22:58
  • @beroe just the question as posed... sorry, I'll fix it. Commented Aug 30, 2014 at 14:13

4 Answers 4

2
+25

You should check your php.ini for line like the next:

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

and such.

Also check your "safe mode" status, (php ver. < 5.4) if you have enabled it you can only execute files within the safe_mode_exec_dir and so on...

More information in the doc and for the execution of commands here and especially for system here.

Sign up to request clarification or add additional context in comments.

2 Comments

My php.ini has disable_functions=, and no lines that have anything to do with 'safe mode'. So this is not the issue, unfortunately.
There are two php.ini sometimes, one for command-line interface and one for apache. /etc/php/7.4/apache2/php.ini or sudo vi /etc/php/7.4/cli/php.ini. Make sure you are modifying the right one. And of course you need to restart apache.
1

echo is invariably a shell internal command, not a separate executable. You need something more like

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

7 Comments

Thanks for the comment, but using cmd.exe -c vfmt -h cat.v still doesn't work...
whoops. sorry. it's /c.
... and neither does cmd.exe /c vfmt -h cat.v. Any other ideas?
vfmt doesn't need the cmd stuff, since it is an executable. try exec('vfmt ...', $output, $ret); var_dump($output).
That's what I thought. So that leads back to my question of "why doesn't vfmt -h cat.v work in a PHP system call when it does in cmd.exe?
|
1

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

This is hidden as you're checking return value and not screen output; they are different.

Firstly, to monitor: try using exec https://www.php.net/manual/en/function.exec.php as this will help you debug by giving both screen output and return value.

Secondly, to fix:

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

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

or

2 - Change the working directory before calling system() or exec() by using chdir() first.

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

Comments

1

You need to redirect the output of the command you're running with system() in order for it to run in the background. Otherwise, PHP will wait for program execution to end before continuing.

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

Not sure how you're invoking your script, but perhaps you're running into a PHP max_execution_time (or related) limit. Something to investigate. Also, since you're running on windows, you may need to use absolute paths with your commands when executing system calls as the file you're invoking may not be in any defined PATH.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.