I am using WAMP server on my system to execute php scripts.
I want to execute a script test.php from my main script main.php.
For that I am using exec function like this exec('php test.php');. In test.php I have given one echo statement.
But when I run my main.php script from the browser I am not able to see output of test.php script.
What am I doing wrong ? please suggest.
You have to give the proper path of php.exe
exec("c:\wamp\<where ever you exe is>/php.exe test.php");
so it has to be a proper path
use this command
echo exec('php test.php', $output); //this will print your echo statement.
print_r($output);
Related
I am using XAMPP on Windows 7 to run a localhost, and I'm trying to use the PHP exec function to execute a Node.js script.
I am able to successfully use the exec function to run PhantomJS scripts, but when I try to do the same thing for Node.js, it doesn't work.
He's an example of a PHP script that properly runs a PhantomJS script:
<?php
exec('/phantomjs/bin/phantomjs /phantomjs/scripts/test.js', $output);
print_r($output);
And here's a similar example of a Node.js script that outputs an empty array every time:
<?php
exec('/Program Files (x86)/nodejs/node /Program Files (x86)/nodejs/test.js', $output);
print_r($output);
I'm sure that all of my paths are correct and that the Node.js script executes correctly whenever I execute it directly from the command line, but I can't get anything to return from the Node.js script when I run it from the PHP exec command. I also tried the following script, but I still get nothing:
<?php
exec('/Program Files (x86)/nodejs/node -v', $output);
print_r($output);
Any advice on what I'm missing would be greatly appreciated. Thank you.
I am trying to use a PHP script to call a shell script and print the output to the browser.
I have confirmed that all permissions settings are set correctly, both the PHP and the bash script have full control.
If I run the shell script in the current directory (same directory as the PHP file) then it works fine. But if I try to run the shell script from /some/other/directory/shell.script, then it does not work. Why is this so?
I have tried to chdir() to the other directory, but after running a getcwd(), it never changed directories.
I have also tried the exec command by itself and it does not work.
<?php error_reporting(E_ALL); ini_set('display_errors',1);
echo "<h1>EDS Count Report</h1><p>";
$output=shell_exec('cd /app/script/catalog/ && ./EDScount_report.sh');
echo $output;
?>
contents of SHELL script:
#!/bin/bash
echo "this is a test!"
Try this please
<?php
$output=shell_exec('sh /path/to/otherdirectory/shellscript.sh');
echo $output;
?>
if you are certain that you can access that dicertory , you can use also this:
<?php
$output=shell_exec('cd /path/to/otherdirectory/ && ./shellscript.sh');
echo $output;
?>
i am new to exec function and need help with executing an external php file. My code of the files are as follows
Script.php(main file) :
<?php
$path = 'C:/xampp/htdocs/user/execute.php';
exec($path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>
execute.php(calling file) :
for($i=0;$i<10;$i++){
echo "hello"."<br>";
}
trying to execute the calling file
exec is for executing system functions, not for running scripts. (Take a look at the manual, it's helping: http://php.net/manual/de/function.exec.php)
To achieve what you want, you could pass the path to php executable and add the script as parameter, like this:
<?php
$phpExecutable = 'C:/xampp/bin/php.exe'
$path = 'C:/xampp/htdocs/user/execute.php';
exec($phpExecutable." ".$path, $output,$return);
var_dump($return);
echo "hi"."<br>";
echo "end";?>
Should work. I do not know where your php executable is located, so please adapt it to your location.
Happy coding.
First of all, as guys said in comments, you don't need exec() here at all. You can just include that file.
Anyway, exec() function executes external program. A .php file is not a program, it's just a script that is executed by a program called php.
So you can run it like:
exec('php ' . $path, $output,$return);
php also can require you to give full path to its executable if it's not available globally.
http://php.net/manual/en/function.exec.php
Adding
exec("php execute.php > /dev/null &");
solved my problem..Thanks all
I am trying to call a simple python script
#!/usr/local/python25/bin/python
print "hello world"
from the following php script
<?php
echo exec("/usr/local/python25/bin/python myfile.py");
?>
But nothing was happened.
Please tell me what is wrong here? (I also checked other thread but I could not solve my problem)
Question Solved:
I forgot to give the permission to access /usr/local/python25/bin/python. After I did this, the problem solved.
Thank you so much for your help!
1.The exec function just return the last line from the result of the command.
2. The print statement in python (except python 3) automatically adds a newline at the end.
This is the reason you feel nothing was happened.
You can catch the whole output by this way.
exec("/usr/local/python25/bin/python myfile.py 2>&1", $output);
print_r($output);
Kind of an obvious point here, but can you run the python script from a terminal? Does it actually run?
Make sure the script is executable by whatever user PHP is running as - chmod 777 myfile.py, and just to be safe chmod 777 /usr/local/python25/bin/python. Also, make sure the python script is in the same directory as the PHP script, which is what your method of calling it requires.
Try changing your PHP script to this, and tell me what you see: (EDITED)
<?php
// Path to the python script - either FULL path or relative to PHP script
$pythonScript = 'myfile.py';
// Path to python executable - either FULL path or relative to PHP script
$pythonExec = '/usr/local/python25/bin/python';
// Check the file exists and PHP has permission to execute it
clearstatcache();
if (!file_exists($pythonExec)) {
exit("The python executable '$pythonExec' does not exist!");
}
if (!is_executable($pythonExec)) {
exit(("The python executable '$pythonExec' is not executable!"));
}
if (!file_exists($pythonScript)) {
exit("The python script file '$pythonScript' does not exist!");
}
// Execute it, and redirect STDERR to STDOUT so we can see error messages as well
exec("$pythonExec \"$pythonScript\" 2>&1", $output);
// Show the output of the script
print_r($output);
?>
If you want to capture the subprocess' stdout, you should use passthru
Also you don't need the first line of that python script if you're calling the python interpreter directly.
I am trying to invoke a script which takes several seconds (web services with 3rd party) using the PHP exec call. After much struggling, I reduced this to the classic hello world example. The calling script looks like:
exec('/usr/bin/php /home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &');
When I run this, the output execoutput.txt contains a copy of the invoking script page, not hello world as I expected.
Why can't I get this PHP script to execute using exec? Note that when I change the command to something like ls -l, the output is a directory listing as expected. btw, in case it matters, I did chmod the called script to 755...
Update - I moved the exec call to the end of the calling script and at least now I don't see the calling script executed in the output. Thx to posters and I will try some of these ideas.
Help!
Thanks
Steve
I had this issue also and it turns out this is a bug in php (#11430). The fix is to use php-cli when calling another php script within a php script. So you can still use exec but rather than use php use php-cli when calling it in the browser:
exec("php-cli somescript.php");
This worked for me.
What exec is doing is taking the rightmost command and appending it to your destination. If you have the shebang line in your php script, you shouldn't need to include the binary directive of the php interpreter.
if you just want the script's output, try:
exec('/home/quote2bi/tmp/helloworld.php > /tmp/execoutput.txt 2>&1 &')
however if you do not want the errors to be in the file, you should redirect the STDERR prior to outputting to the file. Like so:
exec('/home/quote2bi/tmp/helloworld.php 2> /dev/null > /tmp/execoutput.txt')
the above should only output the "Hello World" to the execoutput.
Edit:
Interesting you are getting this behaviour. You stated the command "ls" worked. Try making an alias for this and forward it to a file like so:
alias pexec='php /home/quote2bi/tmp/helloworld.php'
then
exec('pexec > /tmp/execoutput.txt 2>&1 &')
it seems to be a problem with the way exec handles input as opposed to the shell itself.
-John
The problem is with PHP itself, it treats everything as $argv in the script. It doesn´t redirect the output to a file ou to /dev/null.
I faced the same problem some time ago. What I did is to create a runscript.php in /opt/php-bin and then inside this script run what It should be running. Something like this:
$script = $argv[1]
$params = implode(' ', array_slice($argv, 2));
$cmd = "{$script} {$params} > /dev/null &";
$output = array();
$return = 0;
exec("php {$cmd}", $output, $return);
exit((int)$return);
And then you call it using:
exec('/opt/php-bin/runscript.php /path/to/your/script.php arg1 arg2')
It´s the only way I managed to get this working.
To avoid the stated problems of PHP in this area, why not put this in inside a shell script? PHP can then execute the shell script which has all the redirections handled internally.
If you need to dynamically change things, then why not write the shell script and then execute it (and of course, clean up afterwards)?
if you are just simply running a php script one possible way to execute the entire code is to use the include() that will run the php file and output any results. You cannot direct the output to a text file but it should appear in the browser window if you're Hello World php script looks like
<?php echo "Hello World!"; ?>
then it will spit that out in the browser. So your second code would look like
<?php include("helloWorld.php"); echo " PHP ROCKS";?>
resulting in a page that would look like,
Hello world! PHP ROCKS
This runs as if you run the script from browser.
This came across while working on a project on linux platform.
exec('wget http://<url to the php script>)
Hope this helps!!