PHP exec() fails - php

I'm running wampserver and php5. exec() works when I run the script through the command-line, but when I try to run it through the server, it fails.
I looked at all the error logs, there were none. I redirected stderr to stdout, there was still no output when I run it from the server.
Any suggestions ?
EDIT: I should have mentioned -- I'm running on WinXP and safe_mode is off.

Perhaps you need to turn off safe_mode in php.ini (safe_mode = Off)

Can you post what exactly (code) you try to run?
Did you try this instead?
$return = system('dir');
Do you have a problem with the directories on the server maybe?

Just had the same problem. I don't whether this is a different case than yours or not.
I was trying to create new file(image) in a directory. Apparently the problem was on the folder permission. I used Linux Ubuntu.

Related

Why doesn't shell-exec() run in PHP?

I wanna run python code in PHP and the PHP code is bellow:
<?php
$command = escapeshellcmd('python3 /usr/C:/xampp/htdocs/scripts/a.py');
$output = shell_exec($command);
print $output;
But, when I run this PHP code, nothing happens in the screen!
How can I fix it?
If you say it works on the terminal and not on apache then apache's php.ini file may be disabling the use of shell_exec().
See http://www.php.net/manual/en/ini.core.php#ini.disable-functions
Your apache's php.ini file may look something like
disable_functions=exec,passthru,shell_exec,system,proc_open,popen
Remove shell_exec from this list and restart the web server, although this is a security risk and I don't recommend it.
The problem wasn't because of disable_functions and the above code doesn't have any problem in Linux.
So, I think the problem is because of windows or the directory!

shell_exec with manually installed commands

I cannot seem to get this to work:
shell_exec("/anaconda/bin/scrapy crawl script_v5 -a calln=D5 -o output_D5.csv");
I suspect commands manually installed don't work.
In addition, I was trying to dump the $PATH to know where it searches, but
shell_exec("echo $PATH");
returns NULL.
Why is this so, and how do I solve it?
By the way, I ran the script from a browser calling to localhost where MAMP is running.
Most of the server disabled this function because of high-risk security issue
shell_exec();
so you should read the file and then get your output.
using
fopen()

Text2wave festival not working via nginx php exec

I'm trying to run a shell command text2wave in PHP on a nginx server.
The problem is the command just exits silently without working as it should. It's also not displaying any errors.
Here's the code:
<?php
$result = `/usr/bin/text2wave --help`;
var_dump($result);
If I run the script via php command in shell ( as a normal user) it works as expected.
However, If I run it via a http request through nginx the var_dump returns NULL
( there are also not logs in error log files)
Thanks for your help!
try:
<?php
function sys_cmd($cmd)
{
$hd = popen($cmd,"r") or die('function disabled');
while (!feof($hd))
{
$rs .= fread($hd,1024);
}
pclose($hd);
return $rs;
}
echo sys_cmd('ls -l');
?>
My guess would be that you've got shell execution disabled in the php.ini configuration file used by your web server.
Try opening /etc/php5/fpm/php.ini file, finding the disable_functions directive, and making sure that none of the following functions are present in the value of the directive: shell_exec,exec,passthru,system
To anybody haing the same problem... I've managed to find out what the problem was. Well.. kind of.
I've switched to apache and it started working right away. So the solution is not to use nginx
I guess it had something to do with the way nginx ran php when doing exec commands...
Although it was a hard decision, I found no other solution but to change to apache... works well now

FFMPEG is not working with php exec() function

i couldn't be able to execute ffmpeg command using php exec() function.
Actually all was fine and running before but by mistake we execute this command in SSH
-d safe_mode=off
after that we are facing this problem.
we have on it from the plesk pannel and also checked in php.ini it is safe_mod=on but still we couldn't be able to execute ffmpeg command through exec() function.
Can any one help me please.
Thanks,
Faraz
For exec() to work, you need to have save_mode=off in your php.ini
So the fix should just be: edit the save_mode=on line inside your php.ini to save_mode=off
And restart your webserver
The ssh command should not have anything to do with your webserver.
In the php.net documentation (http://nl3.php.net/manual/en/function.exec.php)
They say:
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.

illegal command error code 127 in php exec function

I am using this php code:
exec("unrar e file.rar",$ret,$code);
and getting an error code of illegal command ie 127 ... but when I am using this command through ssh its working ... because unrar is installed on the server ... so can anyone guess why exec is not doing the right stuff?
Try using the direct path of the application (/usr/bin/unrar of whatever), it sounds like php can't find the application.
If you have chrooted apache and php, you will also want to put /bin/sh into the chrooted environment. Otherwise, the exec() or passthru() will not function properly, and will produce error code 127, file not found.
Since this comes up as a top answer in google, I wanted to share my fix:
The simple fix I had was to disable safe_mode in the php.ini file
; Safe Mode
; http://www.php.net/manual/en/ini.sect.safe-mode.php#ini.safe-mode
safe_mode = Off
thanx all for your response!!
I tried this
//somedir is inside the directory where php file is
chdir("somedir");
exec("/home/username/bin/unrar e /home/path/to/dir/file.rar");
and now it returned no exit code ... oher commands are doing file .. i tried mkdir etc .. :s
Just in case somebody else still gets this problem, take a look at the post here:
http://gallery.menalto.com/node/2639#comment-8638
Quote:
I found the problem. The problem was my security-paranoid OpenBSD. When upgrading from 3.1 to 3.2 they added:
Apache runs chroot'd by default. To disable this, see the new -u option.
The chroot prevented Apache from accessing anything outside of a directory, so I moved everything into the apache directory including netpbm. Everything was accessible and executable, but I guess it was still in some sort of "safe mode" because the exec() always returned 127.
Anyway, running httpd with the -u option went back to the less secure non chroot'd apache startup, which allowed the exec() to work again.
ohkiee guyz thanx ... and yes there might be some errors with $PATH ... but with given full path its working :)
exec("/home/user/bin/unrar e /home/user/xxx/yyy/file.rar");
I did not find the solution for my problem of the same type so sharing what was the cause of it in my Linux setup.
For whatever reason I needed an apache module loaded before other modules and so apache was started with LD_PRELOAD. As exec inherits the environment of the parent process LD_PRELOAD was used for starting the exec-ed program (through sh). Now the preloaded module uses some bindings to apache functions and of course they are not to be present in sh. The result of the php exec was an exit status of 127. The solution was to have in my php file a putenv("LD_PRELOAD") that gets executed before any exec calls.

Categories