Problem scenario
You have a PHP program that works from the back-end (from a command terminal executing like php foobar.php
). When you view the program in a web browser, it is not running the Bash commands properly. What should you do?
Solution
The root cause is probably the user execution context and a variety of permissions or configuration settings on the Linux system.
If the output of the Bash command is multiple lines, you may be capturing only the last line and sending that to a variable in PHP. If the last line was empty, you may see just white.
The user account that invokes the PHP program when it is executed via someone browsing to it on a web page is different from your back-end user account. This may be what is wrong. Here is one important diagnostic for you. Create a file called good.php and place it where a user can browse to it (e.g., /var/www/html/).
<?php
$output = shell_exec('/usr/bin/env | /usr/bin/xargs');
echo $output;
?>
<html>
<body>
HELLO!
</body>
</html>
When you go to the above web page in a web browser, you will get an idea of what issues your PHP program is running into when it is executed from someone browsing to it. (The apache or nginx users may be executing the PHP program; these users may not have sufficient back-end permissions on the Linux system.) You may need to change the location of the "env" executable in the program above; you could use which env
to find the appropriate location. Here is another tip if Linux commands are not processing properly from a PHP program: the /tmp/ directory has the sticky bit set. So you should choose a different directory to write to. You may want to run sudo ls -lhd /foobar
(where "foobar" is the destination directory where the bash script is supposed to write to).