How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?
I have a phpinfo.php
file which is accessed from the web shows that German
is installed. However, if I run the phpinfo.php
from the command line using - php phpinfo.php
and grep
for German
, I don't find it. So both PHP files are different. I need to run a script which the php
on which German
is installed.
How can I do this?
As of version 4.3. 0, PHP supports a new SAPI type (Server Application Programming Interface) named CLI which means Command Line Interface. As the name implies, this SAPI type main focus is on developing shell (or desktop as well) applications with PHP.
php” file is placed inside the “htdocs” folder. If you want to run it, open any web browser and enter “localhost/demo. php” and press enter. Your program will run.
You can execute linux commands within a php script - all you have to do is put the command line in brackits (`). And also concentrate on exec() , this and shell_exec() .. Save this answer.
Go to php.net and download the php file stack for windows. Copy the file stack into say c:\php or if you want multiple versions, say c:\php5 or c:\php7 etc. Open powershell and type c:\php\php.exe -h , you will get the php help output. Yay you are up and running, whoot.
You should check your server configuration files. Look for lines that start with LoadModule php
... There probably are configuration files/directories named mods
or something like that. Start from there.
You could also check output from php -r 'phpinfo();' | grep php
and compare lines to phpinfo();
from web server.
php
interactively:(So you can paste/write code in the console.)
php -a
php -f file.php
php -f file.php > results.html
To run only a small part, one line or like, you can use:
php -r '$x = "Hello World"; echo "$x\n";'
If you are running Linux then do man php
at the console.
If you need/want to run PHP through fpm (FastCGI Process Manager), use cli fcgi:
SCRIPT_NAME="file.php" SCRIP_FILENAME="file.php" REQUEST_METHOD="GET" cgi-fcgi -bind -connect "/var/run/php-fpm/php-fpm.sock"
Where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.
On SUSE Linux, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). In the /etc/php5/ directory, you will find an "apache2" directory and a "cli" directory. Each has a "php.ini" file. The files are for the same purpose (PHP configuration), but apply to the two different ways of running PHP. These files, among other things, load the modules PHP uses.
If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the German module, while the the CLI php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.
You could simply copy the Apache php.ini file over into the cli
directory to make the CLI environment exactly like the Apache environment.
Or, you could find the line that loads the German module in the Apache file and copy/paste just it to the CLI file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With