Shell run/execute php script with parameters

前端 未结 5 1768
独厮守ぢ
独厮守ぢ 2020-12-02 22:59

I need to execute a php file with parameters through shell.

here is how I would run the php file:

php -q htdocs/file.php

5条回答
  •  情歌与酒
    2020-12-02 23:19

    If you are using it from a PHP file then you can use popen() and do something like this:

    $part = $show_name; //or whatever you want with spaces
    
    $handle = popen("php -q nah.php -p=". escapeshellarg($part) . " 2>&1", "r");
    

    This uses the escapeshellarg() function in order to wrap the $part variable in quotes (and escape any quotes inside it), so that it can be used as a shell argument safely.

提交回复
热议问题