PHP 外部执行函数

PHP 外部执行函数:exec()、system()、passthru()、shell_exec()、popen()、proc_open()、执行运算符(反引号)、pcntl_exec()

大致可分成三部分

1)exec()、system()、passthru()

2)shell_exec()、popen()、proc_open()、执行运算符(反引号)

3)pcntl_exec()

1)exec()、system()、passthru()

  • exec:执行一个外部程序,只输出最后一行

  • system:执行外部程序,并且显示输出

  • passthru:执行外部程序并且显示原始输出

Notes

  • passthru 与 system 输出结果比 exec 更全面,passthru 适用范围比 system 更广

2)shell_exec()、popen()、proc_open()、执行运算符(反引号)

  • shell_exec:通过 shell 执行命令并将完整的输出以字符串的方式返回。本函数同执行运算符

    • 在 Windows 上,底层管道以文本模式打开,这可能导致函数无法进行二进制输出。考虑使用 popen() 避免这种情况。
  • 执行运算符:反引号(``)。

    • PHP 将尝试将反引号中的内容作为 shell 命令来执行,并将其输出信息返回。

    • 反引号不能在双引号字符串中使用。

  • $():与反引号类似

  • popen:打开进程文件指针

  • proc_open:执行一个命令,并且打开用来输入/输出的文件指针。(类似 popen() 函数,但是 proc_open() 提供了更加强大的控制程序执行的能力。 )

Notes

  • 反引号和 $() 常被统称为内敛执行

  • 若关闭shell_exec(),则反引号也无法使用



3)pcntl_exec()

  • pcntl_exec:在当前进程空间执行指定程序。pcntl 类的函数
posted @ 2023-07-25 16:11  kazie  阅读(70)  评论(0编辑  收藏  举报