Php exec not working from browser

I am running an SCO Unix box with apache version 1.3.33 and PHP version 4.4. I can properly execute the exec command through the cli, but run into trouble with trying to execute the script via a browser. My settings are:

  • safe mode off
  • full read/write/exec permissions
  • displaying all errors
  • no disabled functions

My code: test_script.php in htdocs dir


I've tried setting the executable binary path explicitly also. The test script is the same User/Group as apache. I've also tried adding 2>&1 to the arg, but see no change. Thank you in advance.

See here for similar.

asked Mar 14, 2011 at 23:28

7

try

$output = shell_exec['ls /tmp/ 2>&1']

you might be having an issue with permissions to current directory for the user running the web page.

you can also try whoami command.

answered Mar 20, 2011 at 18:20

ShoshanShoshan

3111 silver badge4 bronze badges

Sometimes these functions are disabled, you have to enable them in php.ini

answered Mar 27, 2013 at 1:01

TeodorosTeodoros

3793 silver badges12 bronze badges

Server : Ubantu 16 Php version : 7

i have php 7 installed on ubantu 16 .i have also installed imagemagick.

now my php file say index.php with code below

echo exec["whoami"];
echo exec["/usr/bin/convert '/var/www/html/slc.png' '/var/www/html/slc.ppm'"];

now when i run it in browser say example.com/index.php , it show output

www-data

But convert command does not gets executed.

but when i run it via cli new ppm file is created

php index.php

can you please tell me how to fix this issue

Question

I have script that create folder for any user that sign up, and the script working perfectly fine on local, after pushing to server it refuse to work.

$createDir =  "cd ../../merchants && mkdir ".$merchant->merchant_id" && cd ".$merchant->merchant_id;
exec[$createDir];

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

[PHP 4, PHP 5, PHP 7, PHP 8]

shell_execExecute command via shell and return the complete output as a string

Description

shell_exec[string $command]: string|false|null

Note:

On Windows, the underlying pipe is opened in text mode which can cause the function to fail for binary output. Consider to use popen[] instead for such cases.

Parameters

command

The command that will be executed.

Return Values

A string containing the output from the executed command, false if the pipe cannot be established or null if an error occurs or the command produces no output.

Note:

This function can return null both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec[] should be used when access to the program exit code is required.

Errors/Exceptions

An E_WARNING level error is generated when the pipe cannot be established.

Examples

Example #1 A shell_exec[] example

See Also

  • exec[] - Execute an external program
  • escapeshellcmd[] - Escape shell metacharacters

trev at dedicate.co.uk

10 years ago

If you're trying to run a command such as "gunzip -t" in shell_exec and getting an empty result, you might need to add 2>&1 to the end of the command, eg:

Won't always work:
echo shell_exec["gunzip -c -t $path_to_backup_file"];

Should work:
echo shell_exec["gunzip -c -t $path_to_backup_file 2>&1"];

In the above example, a line break at the beginning of the gunzip output seemed to prevent shell_exec printing anything else. Hope this saves someone else an hour or two.

smcbride at msn dot com

1 year ago

proc_open is probably a better solution for most use cases as of PHP 7.4.  There is better control and platform independence.  If you still want to use shell_exec[], I like to wrap it with a function that allows better control.

Something like below solves some problems with background process issues on apache/php.  It also

public function sh_exec[string $cmd, string $outputfile = "", string $pidfile = "", bool $mergestderror = true, bool $bg = false] {
  $fullcmd = $cmd;
  if[strlen[$outputfile] > 0] $fullcmd .= " >> " . $outputfile;
  if[$mergestderror] $fullcmd .= " 2>&1";
  if[$bg] {
    $fullcmd = "nohup " . $fullcmd . " &";
    if[strlen[$pidfile]] $fullcmd .= " echo $! > " . $pidfile;
  } else {
    if[strlen[$pidfile] > 0] $fullcmd .= "; echo $$ > " . $pidfile;
  }
  shell_exec[$fullcmd];
}

alexandre dot schmidt at gmail dot com

6 years ago

To run a command in background, the output must be redirected to /dev/null. This is written in exec[] manual page. There are cases where you need the output to be logged somewhere else though. Redirecting the output to a file like this didn't work for me:

Chủ Đề