简体   繁体   English

从命令行运行PHP脚本

[英]Running PHP script from the command line

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. 我有一个phpinfo.php文件,可以从Web上访问,显示安装了German However, if I run the phpinfo.php from the command line using - php phpinfo.php and grep for German , I don't find it. 但是,如果我运行phpinfo.php使用命令行- php phpinfo.phpgrepGerman ,我并不觉得。 So both phps are different. 所以两个phps都不同。 I need to run a script which the php on which German is installed. 我需要运行一个安装了Germanphp脚本。

How can I do this? 我怎样才能做到这一点?

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 您还可以检查php -r 'phpinfo();' | grep php php -r 'phpinfo();' | grep php php -r 'phpinfo();' | grep php and compare lines to phpinfo(); php -r 'phpinfo();' | grep php并将行与phpinfo();进行比较phpinfo(); from web server. 来自网络服务器。

To run php interactively: 以交互方式运行php

(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 > 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";'

If you are running linux then do man php at console. 如果你正在运行linux,那么在控制台上做man php

On SuSE, there are two different configuration files for PHP: one for Apache, and one for CLI (command line interface). 在SuSE上,PHP有两种不同的配置文件:一种用于Apache,另一种用于CLI(命令行界面)。 In the /etc/php5/ directory, you will find an "apache2" directory and a "cli" directory. 在/ etc / php5 /目录中,您将找到“apache2”目录和“cli”目录。 Each has a "php.ini" file. 每个都有一个“php.ini”文件。 The files are for the same purpose (php configuration), but apply to the two different ways of running PHP. 这些文件用于相同的目的(php配置),但适用于运行PHP的两种不同方式。 These files, among other things, load the modules PHP uses. 除其他外,这些文件加载​​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. 您的Apache php.ini可能正在加载gearman模块,而cli php.ini则不是。 When the module was installed (auto or manual), it probably only updated the Apache php.ini file. 安装模块(自动或手动)时,它可能只更新了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文件中。

I was looking for a resolution to this issue in Windows, and it seems to be that if you don't have the environments vars ok, you need to put the complete directory. 我在Windows中寻找解决此问题的方法,似乎如果你没有环境vars ok,你需要放置完整的目录。 For eg. 例如。 with a file in the same directory than PHP: 与PHP目录相同的文件:

F:\myfolder\php\php.exe -f F:\myfolder\php\script.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM