从命令行运行PHP脚本 [英] Running PHP script from the command line

查看:187
本文介绍了从命令行运行PHP脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用用于解析Web脚本的PHP解释器从命令行运行PHP脚本?

How can I run a PHP script from the command line using the PHP interpreter which is used to parse web scripts?

我有一个phpinfo.php文件,可以从Web上访问该文件,表明已安装German.但是,如果我使用-php phpinfo.phpgrep表示German从命令行运行phpinfo.php,则找不到它.因此,两个php都是不同的.我需要运行安装了Germanphp脚本.

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 phps are different. I need to run a script which the php on which German is installed.

我该怎么做?

推荐答案

更新:

经过误会,我终于明白了你要做什么. 您应该检查服务器配置文件.您在使用apache2或其他服务器软件吗?

UPDATE:

After misunderstanding, I finally got what you are trying to do. You should check your server configuration files; are you using apache2 or some other server software?

查找以LoadModule php开头的行... 从那里开始可能有名为mods或类似名称的配置文件/目录.

Look for lines that start with LoadModule php... There probably are configuration files/directories named mods or something like that, start from there.

您还可以检查php -r 'phpinfo();' | grep php的输出,并将行与Web服务器中的phpinfo();进行比较.

You could also check output from php -r 'phpinfo();' | grep php and compare lines to phpinfo(); from web server.

(因此您可以在控制台中粘贴/编写代码)

(so you can paste/write code in the console)

php -a

要使其解析文件并输出到控制台:

php -f file.php

解析文件并输出到另一个文件:

php -f file.php > results.html

您还需要其他东西吗?

要只运行一小部分,一行或类似内容,可以使用:

Do you need something else?

To run only small part, one line or like, you can use:

php -r '$x = "Hello World"; echo "$x\n";'

如果您正在运行linux,请在控制台上执行man php.

If you are running linux then do man php at console.

如果您需要/希望通过fpm运行php,请使用cli fcgi

if you need/want to run php through fpm, 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"

其中/var/run/php-fpm/php-fpm.sock是您的php-fpm套接字文件.

where /var/run/php-fpm/php-fpm.sock is your php-fpm socket file.

这篇关于从命令行运行PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆