从命令行运行PHP脚本

时间:2021-02-19 01:47:24

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

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

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.

我有一个phpinfo.php文件,可以从Web*问,显示安装了德语。但是,如果我使用 - php phpinfo.php和grep for German运行命令行中的phpinfo.php,我找不到它。所以两个phps都不同。我需要运行一个安装了德语的php的脚本。

How can I do this?

我怎样才能做到这一点?

2 个解决方案

#1


84  

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?

在误解之后,我终于得到了你想要做的事情。您应该检查您的服务器配置文件;你在使用apache2或其他服务器软件吗?

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

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

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

您还可以检查php -r'phpinfo();'的输出| grep php并将行与phpinfo()进行比较;来自网络服务器。

To run php interactively:

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

(所以你可以在控制台中粘贴/编写代码)

php -a

To make it parse file and output to console:

php -f file.php

Parse file and output to another file:

php -f file.php > reults.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";'

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

如果你正在运行linux,那么在控制台上做man php。

#2


1  

(I know this is an old question)

(我知道这是一个老问题)

On SuSE, 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.

在SuSE上,PHP有两种不同的配置文件:一种用于Apache,一种用于CLI(命令行界面)。在/ etc / php5 /目录中,您将找到“apache2”目录和“cli”目录。每个都有一个“php.ini”文件。这些文件用于相同的目的(php配置),但适用于运行PHP的两种不同方式。除其他外,这些文件加载​​PHP使用的模块。

If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the gearman module, while the cli php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.

如果您的操作系统类似,那么这两个文件可能不一样。您的Apache php.ini可能正在加载gearman模块,而cli php.ini则不是。安装模块(自动或手动)时,它可能只更新了Apache php.ini文件。

You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.

您可以简单地将Apache php.ini文件复制到cli目录中,以使CLI环境与Apache环境完全相同。

Or, you could find the line that loads the gearman module in the Apache file and copy/paste just it to the CLI file.

或者,您可以在Apache文件中找到加载gearman模块的行,并将其复制/粘贴到CLI文件中。

#1


84  

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?

在误解之后,我终于得到了你想要做的事情。您应该检查您的服务器配置文件;你在使用apache2或其他服务器软件吗?

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

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

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

您还可以检查php -r'phpinfo();'的输出| grep php并将行与phpinfo()进行比较;来自网络服务器。

To run php interactively:

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

(所以你可以在控制台中粘贴/编写代码)

php -a

To make it parse file and output to console:

php -f file.php

Parse file and output to another file:

php -f file.php > reults.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";'

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

如果你正在运行linux,那么在控制台上做man php。

#2


1  

(I know this is an old question)

(我知道这是一个老问题)

On SuSE, 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.

在SuSE上,PHP有两种不同的配置文件:一种用于Apache,一种用于CLI(命令行界面)。在/ etc / php5 /目录中,您将找到“apache2”目录和“cli”目录。每个都有一个“php.ini”文件。这些文件用于相同的目的(php配置),但适用于运行PHP的两种不同方式。除其他外,这些文件加载​​PHP使用的模块。

If your OS is similar, then these two files are probably not the same. Your Apache php.ini is probably loading the gearman module, while the cli php.ini isn't. When the module was installed (auto or manual), it probably only updated the Apache php.ini file.

如果您的操作系统类似,那么这两个文件可能不一样。您的Apache php.ini可能正在加载gearman模块,而cli php.ini则不是。安装模块(自动或手动)时,它可能只更新了Apache php.ini文件。

You could simply copy the Apache php.ini file over into the cli directory to make the CLI environment exactly like the Apache environment.

您可以简单地将Apache php.ini文件复制到cli目录中,以使CLI环境与Apache环境完全相同。

Or, you could find the line that loads the gearman module in the Apache file and copy/paste just it to the CLI file.

或者,您可以在Apache文件中找到加载gearman模块的行,并将其复制/粘贴到CLI文件中。