system(), exec() etc command works fine in windows (Windows+XAMPP) but when trying in live server (linux) all functions returns NULL. How can run those function from linux?
I think that you are looking for shell_exec
shell_exec("/path/to");
edit:
sometimes the problem is with escaping, wrap your arguments with
escapeshellarg
Problem could be related with incorrect path or permission settings on Linux.
You can test this function in a php script to show the content of a folder:
<?php
$output = shell_exec('ls -lart 2>&1');
echo "<pre>$output</pre>";
?>
Notice this function is disabled when PHP is running in safe mode.
Hosting administrator may disabled some functions on the server for the security reason.
You may contact them
Related
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!
I'm trying to build an interface which allows entering a query that is to be passed to an R script (found in the same folder) which does some calculation and prints the output:
<?php
$author=$_POST['data'];
echo $author."<br>";
$output = shell_exec("Rscript --vanilla h-index.R '$author' ");
echo "<pre>$output</pre>";
?>
Yet for some reason the script won't fire.. I gave it exec permissions and tested it via the command line and it works.
Any ideas?
OK, I figured out thanks to rickdenhaan that shell did not recognise the 'Rscript' command and using the absolute path /usr/local/bin/Rscript it worked
Most likely the process running php has in its .ini file the directive disabled_functions="shell_exec" and perhaps also others.
This is done for security reasons. Check with your hosting provider whether this is the case.
Note that they most likely won't be willing to change this setting though.
The command line version of php has a different .ini file than the webserver or process manager for php. This is most likely the reason you could use shell_exec when invoking from the command line.
As a side note: You should perform at least some sanitisation on the argument you accept in the $_POST variable.
I'm trying to use a php script for executing a shell script. However I'm having some issues apparently with the web server.
I have a bash script called switch_audio.sh. It basically changes the active audio output of the system.
I also have a script.php that runs the code below:
<?php
echo "It's working";
exec("/var/www/html/switch_audio.sh");
?>
When I execute php script.php it's working fine. However, when I try to run it on the web browser by localhost/script.php I just get as ouput the "echo" part.
I've already tried to:
remove 'exec()' from the disable functions in php.ini;
give permissions for everybody in every folder on this path from "/" to the localhost folder;
Any thoughts about it?
Simple solution: exec() returns a string, but you also have to output it to the user:
<?php
echo "It's working";
echo exec("/var/www/html/switch_audio.sh");
?>
You already said you allowed the shell function on your php.ini configuration but it seems it's not working yet... so maybe:
You didn't restarted webserver service after changes
You have somewhere other statement more restrictive... maybe ini_set in your php files.
As a reminder, be sure that you are doing well on your php.ini file, check it for this kind of statement:
disable_functions=exec,passthru,shell_exec
Maybe you can try instead of exec other similar php function like shell_exec to check if it works. Or maybe is working and not showing anything.
PHP's exec() function is turned off in my hosting server and it's not possible to change it (for security reasons). Are there alternatives for the exec() function?
Here is more info about my problem:
What I want to do: I want to use .jar file in host server.
This is how it looks on my localhost:
function generateReqXML() {
exec('"C:\Program Files\Java\jdk1.7.0_21\bin\java.exe"
-jar vaiisis.jar auth', $out);
}
This .jar file help my system to generate XML code.
This code work perfect for my local machine, but its facing the problem when I am trying to use it in my host server.
Alternatives to exec are:
system(); passthru(); shell_exec();
You can check if these are available with echo ini_get("disable_functions");
But since these are probably disabled there are only non-PHP options left.
Do you have to use the exec() function via PHP?
You could for example write a shell script, which can be invoked via cron to do your job.
What are you exactly trying to accomplish?
<?php
exec("whoami");
?>
I can be more explicit with the code . Although When I'm trying to call the php file with my browser nothing happens (of course I'm using apache and the whole).
Note : The safe_mode is activated, I'm using php5, php interpreter seems to be nice when running other functions, I'm a ubuntu user.
Then what is wrong?
I think you're looking for the echo function. Executing whoami using the exec function will run the program but show you nothing… you want to spit out the result too.
echo exec("whoami");
You have to echo the output of the exec command somewhere.
PHP documentation for exec function contains the example with whoami, look at the echo.
Right in the docs for exec:
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.
If possible, turn off safe mode. Safes you lots of headaches.
Otherwise, is the php file owned by the same user that Apache runs as?
On Ubuntu, this will usually be www-data.
Try:
sudo chown www-data /path/to/you/script.php
Then run again.