I have an external php script named external.php which includes this:
<?php
echo 'External Output';
?>
When I run that on command line with command:
php external.php
I get output as
External Output
But when the same script I execute from my browser's php file named index.php which has this code:
<?php
$exe=exec('php external.php',$out,$ret);
print_r($out);
?>
Then I get no output.
When I modify it as:
<?php
$exe=exec('php external.php 2>&1',$out,$ret);
print_r($out);
?>
Then I get this output:
php: /opt/lampp/lib/libxml2.so.2: version `LIBXML2_2.9.0' not found (required by php)
My question is, how can I execute that "external.php" file in commandline from index.php and get output on my browser (i.e. on index.php) ? I have tried system() function too, it doesn't work as well.
Edit:
I cannot include the external.php in index.php because external.php can take lot of time to execute (more than 10 hours).
Edit#2
Solved it, giving the full path to PHP solved the problem.
Here is an example:
<?php
$exe=exec('full/path/to/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Thankyou Oleg and Scopey for giving me some hint help.
Specify the full path of external.php:
<?php
$exe=exec('php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try also specifying the full path to php on the command line and check that it works:
$/usr/bin/php "/full/path/to/external.php"
If that works, try it also in your php file:
<?php
$exe=exec('/usr/bin/php /full/path/to/external.php',$out,$ret);
print_r($out);
?>
Try running phpinfo() from the command line. Often installations of PHP use a different PHP.ini for CLI usage. Running phpinfo() will show you the location of the ini file in use. It appears as though this ini might not be configured properly for your installation (as a guess)
Related
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.
I have a bash script that works as expected from both the shell and the PHP command line, but not when called from a PHP page in Apache (Raspbian). I.e, this works (PHP command line):
>php exec('/var/www/html/scripts/myBashScript.sh');
But this doesn't (index.php):
<?php
exec('/var/www/html/scripts/myBashScript.sh');
?>
No error messages are displayed and I can't see anything relevant in the Apache server logs. As suggested by other responses, I've also tried:
exec('sh /var/www/html/scripts/myBashScript.sh')
exec('./scripts/myBashScript.sh')
exec('sh ./scripts/myBashScript.sh')
Both script file and its containing folder have rwx permissions for the Apache user (www-data). The script is set to executable. Built-in bash commands work as expected from the php file, i.e. this works:
<?php
echo exec('whoami');
?>
What am I missing?
The problem turned out to be related to non-Bash commands in the scripts rather than to the script itself. See comments above. Thank you #AlexanderO'Mara.
I expected this to be quite simple but I cannot make it work.
I'm trying to run a php script in command line (to finally be able to automate it in a cron.)
The file is outside of the public web folder to avoid executing it from a webbrowser.
File is called 'daily.php' and contains :
#!/usr/bin/php
<?
echo "hello \n";
file_put_contents("daily.log", "executed...", FILE_APPEND | LOCK_EX);
?>
then in my terminal I run (as root):
/usr/bin/php daily.php
but it simple outputs me the full source code without the hashbang.
So I tried changing the file to 755, tried to chmod +x it but still outputs me the full source code.
I had a look in the man page and I found :
-f <file> Parse and execute <file>.
Tried this but still the same output.
Why is this? how can I interpret this file?
Ok
I found the answer after 2 hours of trouble, and just after posting this question.
Even if you hashbang the file, even if you call php directly, you need to implicitly tell it is php by using :
<?php
?>
and not
<?
?>
Hello I have php working fine with my apache server. However, when executing the php script from the command line (like /usr/bin/php file.php) it just echos the sourcecode of my script back at me, even when executing a PHP in the web directory. Anybody know how to fix this?
Command line scripts STILL need to have at least <?php to switch over to PHP mode. Remember that there's really no such thing as a PHP script. There's only files which have a <?php ... ?> code blocks within them.
So, something like
#!/usr/bin/php
echo 'hello world!';
Would actually output
echo 'hello world!';
because There's no actual PHP code in there. There's just some text that LOOKS like php code. You need to have
#!/usr/bin/php
<?php
echo 'hello world!';
and then you'll get the expected
hello world!
as output. Without <?php the php interpreter will never "switch" over to actual PHP mode, and just treat all of the text in the file as plain output.
In your past questions, you seem to prefer using short open tags (<?), so that is probably the issue. Try running this instead:
/usr/bin/php -d short_open_tag=yes file.php
If this works, then your current config has short_open_tag disabled. You'll have to edit your CLI config file to change this. You can find the location of the config file using this:
/usr/bin/php -i | grep php.ini
You should see:
Configuration File (php.ini) Path => /etc/php5/cli
Loaded Configuration File => /etc/php5/cli/php.ini
Edit the file listed by "Loaded Configuration File". The line with:
short_open_tag = Off
Should be set to On.
I have been fighting this issue for over 4 months and finding nothing to fix it.
My problems started when I installed a new 3T drive and then moved all my projects off the default 1.5T drive. Could never get anything working right in "localhost" since. Also cannot get aliases working in the 2 virtual hosts I created, but their index.html files now show fine at:
projects.com
tbnk-svr.com
But none of the following work:
localhost/phpmyadmin,
localhost/seopanel,
localhost/test.php,
localhost/info.php,
php on the command line.
I wrote this up at:
https://www.linuxquestions.org/questions/newreply.php?do=newreply&noquote=1&p=6214546
and since I get no errors in any of the error logs, can not figure out what is denying localhost and the cmd line from working. Test of:
php <<< "<?php echo 1;"
php -v
Both work, so not sure why it is only echoing the file contents as "cat file" would do.
Has to do somehow with it not rendering "localhost"
Cheers!
TBNK
I'm trying to get the php executable to parse scripts, but it's not working. i run something like this:
php c:\test.php
and test.php contains this:
<?
echo 'hello!';
?>
and that is exactly what running the command returns. It returns the raw code. How can I get it to actually parse the code and return "hello!" instead?
Make sure you have the short_open_tag setting enabled in your php.ini. Failing that, use the ever-so-slightly longer <?php to start your script.
Another option might be to try using the -f command line option.
php -f c:\test.php
It's been a while since I've ran php off the command line, but try this:
<?php
echo 'hello!';
?>
Also make sure that your webserver is correctly configured. If it is not configured to run .php pages through the php interpreter it may simply display as a text file.