简体   繁体   中英

Why won't shell_exec execute files but does execute simple commands?

Is there any reason why I can not complied files in PHP's shell_exec/exec/system function?

Example of something that does work in command line and PHP's shell_exec function:

<?php
$data = shell_exec("ls");
echo $data;
?>

Example of something that does not work in PHP's shell_exec function but will work in command line (I can confirm that):

<?php
$data = shell_exec("./c-compiled-file argv1 argv2 argv3");
echo $data;
?>

Is there anything I can do on my server so this will work? I've looked everywhere and no solutions I found fixed the problem. The compiled file is in the same directory as the PHP script as well, it just won't execute it. Also if you're asking, yes I have tried this with SSH2 and it still will not execute.

Also PHP is not in safe mode and NO functions are disabled.

Some common glitches when executing external commands from PHP that work fine from shell:

  • Command uses relative paths but PHP is launched from an arbitrary location:

  • PHP and shell run with different user credentials. This is often the case when PHP runs through a web server.

  • PHP and shell run different commands. Many people call stuff like exec("foo $bar") and doesn't even check what "foo $bar" contains.

  • No error checking is done. The bare minimum is to capture and print standard output, standard error, status code and, of course, all PHP error messages including warnings and notices.

    • You can redirect stderr to sdtout
    • You can use a PHP function that allows to capture more information, such as exec()
  • The web server is disallowed to execute the command at operating system level.

    • Lookout for SELinux or similar tools.

You want to use system in the second case, and not shell_exec .

system executes an external program and displays the output.

shell_exec executes a command via shell and returns the complete output as a string.

and for good measure:

exec simply executes an external program.

Furthermore you want to make sure your external program is executable and (though you have stated it, I'll restate this) has execute permissions for the user which is running the web server. You also want to make sure the directory your external program is running in has the ability to write to its directory or /tmp or whatever output directory you have set.

Finally you should always use absolute paths for executing things like this in cron or php or whatever... so don't use ./c-compiled-file argv1 argv2 argv3 , but instead use /home/username/c-compiled-file argv1 argv2 argv3 or whatever the full path is.

Just a guess, but the binary you're trying to execute might not have the proper permissions. Prepeding it with ./ in the command line forces it to execute, but PHP probably strips that for security purposes. Try this:

chmod +x c-compiled-file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM