Running PHP script from the command line

后端 未结 3 461
执念已碎
执念已碎 2020-12-04 09:45

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 access

3条回答
  •  日久生厌
    2020-12-04 10:13

    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?

    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.

    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 > 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.

    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"
    

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

提交回复
热议问题