shell_exec()
is not working on my localhost. I read a lot of websites to resolve this issue but can't find a solution.
This code:
$output = shell_exec("env");
print "<pre>$output</pre>";
Doesn't give any output.
I checked in php.ini disable_functions but shell_exec is not disabled.
I did error_reporting = E_ALL but again no output (no error). All I get is just a blank screen. Even safe_mode is off.
If I write echo "BULB"; after the above code it is printing "BULB".
What could be the issue?
What info are you expecting to get from env? From your comments, it seems to me as if you're trying to use a Linux command on a Windows system -- that's never going to work.
On Linux systems, the env command on its own like that returns a list of environment variables that have been defined. However env is not a valid command in Windows.
If you're simply looking for the list of environment variables, this can actually be obtained in PHP without having to go to a shell command. PHP has a built-in global variable $_ENV which contains a copy of all the environment variables that were defined when the program stated. Simply print_r($_ENV) to see them.
On the other hand, if you really need to use shell_exec() for some reason, then you need to take into account the operating system that you're using. On Linux you would use env command. The equivalent on Windows is set without any arguments. So your code becomes:
$output = shell_exec("set");
Note that the format of the output may not be identical to what you'd get on Linux, so if you're parsing it, that code will have to change too.
If you need your code to be able to run on multiple platforms then you would need to write additional code before the shell_exec() call to determine the operating system and work out the correct command to use.
Related
The welcome.js code is given below
console.log('Wellcome');
and the php file code is given below
$op = shell_exec('node welcome.js').PHP_EOL;
echo $op
If I run the php file in command line it print Wellcome but when I run from browser it does not print any output.
There are most likely errors here that you're not seeing.
Set 'error_reporting' to -1 and 'display_errors' to 1 in your php.ini and be sure to restart your webserver/fastcgi-listeners. This is more reliable than using ini_set() and error_reporting() in the script, which will fail if there are parse errors...see php:errorfunc.configuration for more detail.
Check the appropriate error log (depending on your settings and the Server API, this can be the httpd's error log, syslog, some independent file, or even going nowhere right now). Again, php:errorfunc.configuration can help you get things configured correctly, or suss out the current configuration.
The $PATH (or %PATH% on Win32) for an interactive login session is usually dramatically different than that of a running daemon. Try specifying the full path to the node binary.
I don't know off-hand which file-handle node's "console.log()" goes out to. Assuming you're using a bourne-style shell (such as bash) for the subshell here, you can try piping stderr to stdout, using something like: $op = shell_exec('/foo/bin/node welcome.js 2>&1').PHP_EOL; echo $op;
Make sure that 'welcome.js' is where you think it is in relation to the current working directory of your PHP process (although it's likely that node would warn you via one of the previous suggestions if this were not the case, it seemed worth pointing out as a potential pitfall.)
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:
Use getcwd() / chdir() to get/set working directory
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.
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
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.
I am trying to do a simple project to start a VirtualBox VM using a PHP script. My server is win 7 ultimate and running php5.3 I am using the vboxmanage.exe to start the server. cmd works great in a batch file or typed right into cmd line.
When I use:
exec("path to vboxmange.exe" startvm "vm name");
it doesnt work. If I change the code to:
exec(dir);
it works fine. I did some searching and everyone seemed to mention permissions for IIS_IUSRS. I set it to have read and execute on the vboxmanage.exe and tried to do it for cmd.exe but cant seem to get it to allow me to do this, though it doesnt seem necessary as it can run other cmds like dir.
another theory I have is that exec wraps the entire cmd in "" and this doesnt work when I try to type it in to cmd line manually. I have tried to trim it off but that doesnt work cause exec adds it on so nothing I do before can stop this.
any suggestions for another way to do this or what it might be?
From exec in the PHP Manual:
Note: When safe mode is enabled, you can only execute files within the
safe_mode_exec_dir. For practical reasons, it is currently not allowed
to have .. components in the path to the executable.
Check the value of your safe_mode_exec_dir in your php.ini. I would guess that it would be fairly restrictive by default.
If that doesn't work, take a look in the comments. Particularly, this comment might be of help to you.
I've been fighting with this for a few hours now, and I can't seem to work it out.
tried exec(), shell_exec(), and system(). Nothing works.
I have this:
exec("/usr/bin/php /var/www/vhosts/domain.com/httpdocs/shell/send.php >> /var/www/vhosts/domain.com/httpdocs/shell/paging.log &");
send.php simply has:
echo 'works';
But nothing shows up in the log. I've googled and read stuff on here, but I can't find anything to help.
I'm running php v.5.3.8.
safe mode is on
I'm pretty sure that is the path to php, but can't really find out how to find it, so I'm going on phpinfo().
exec('whoami'); does nothing. Is it suppose to show in the browser? or email you something?
any ideas?
According to PHP Manual for exec function:
When safe mode is enabled, you can only execute files within the
safe_mode_exec_dir. For practical reasons, it is currently not allowed
to have .. components in the path to the executable.
Check http://php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode-exec-dir
Also, be aware that the web server user must have permission to write in the log file.
EDIT: To turn safe mode off, check not only php.ini file but also virtual hosts specific configurations in your web server, whether it is Apache, NginX or other. If you use Plesk, look in vhosts for httpd.include, and make sure that safe_mode is set to off there as well.
> The last line from the result of the command. If you need to execute
> a command and have all the data from the command passed directly back
> without any interference, use the passthru() function. To get the output
> of the executed command, be sure to set and use the output parameter.
I am not looking for output, and the last two arguments of exec() are optional. I my case what I really need is to be able to open a folder on the desktop. This exact syntax worked very well in MAMP_PRO_1.9.6, but no longer works in MAMP_PRO_2.0.5 (it's broken)
<?php
exec("open /path/to/any/folder"); // BROKEN in Mamp Pro 2.0.5
?>
To get the output, you need to pass a second parameter, or you can get the last line of output by echo'ing it.
From the PHP manual:
string exec ( string $command [, array &$output [, int &$return_var ]] )
Return Values:
The last line from the result of the command. If you need to execute a command and have all the data from the command passed directly back without any interference, use the passthru() function.
To get the output of the executed command, be sure to set and use the output parameter.
I have a program that returns a comma-separated string of numbers after doing some background processing. I intended to run this in symfony using shell_exec; however, all I get is NULL (revealed through a var_dump(). I tried the following debugging steps.
I ran the file (it's a PHP class) through a command-line lime unit test in Symfony - it works and gives the correct result there.
Just to check, I tried a simple command ls -l at the same place to see whether I would get anything. Again, I had the same problem - the var_dump in the browser showed NULL, but it worked through the command line.
What could be the problem? Are there restrictions on running shell_exec() in a browser?
EDIT: Just to clarify, shell_exec() commands work when I run them as standalone php scripts on the web server (for example, by putting them in my document root. They don't seem to be working under the symfony framework, for some reason.
I finally solved it, and it turned out to be something quite simple, and quite unrelated.
The shell command I was running was in this format: face_query -D args. I didn't realize that Apache would be executing PHP as user www-data and thus the program face_query wouldn't be in the PATH (the directory is actually ~/bin). Changing the program name to the full path of the program solved it.
I also gather from this that only commands which www-data has permission to execute can be run. In this case, www-data is in the same group as my user, but it might be a problem otherwise.
Have you tried using exec? Or one of the other variants. I am never sure of which one to use and always lump with exec.
http://uk.php.net/manual/en/function.exec.php
Is your web server running php in safe mode?
Note: This function is disabled when PHP is running in safe mode.
From: http://php.net/manual/en/function.shell-exec.php