PHP proc_open详解
来源:互联网 发布:御姐控 知乎 编辑:程序博客网 时间:2024/05/17 06:43
php文件的执行,一般都是顺序的。因此当我们的php文件在执行过程中,触发Fatal Error,或者抛出未被捕获的异常时,php文件就会停止执行,因此我们需要使用proc_open函数来分离主脚本和运行脚本。
1.文档实例:
$descriptorspec = array(0 => array("pipe", "r"),//标准输入,子进程从此管道读取数据1 => array("pipe", "w"),//标准输出,子进程向此管道写入数据2 => array("file", "/opt/figli/php/error-output.txt","a")//标准错误,写入到指定文件);$process = proc_open("php", $descriptorspec, $pipes);if(is_resource($process)){fwrite($pipes[0], "<?php echo 'Hello figli!';?>");fclose($pipes[0]);echo stream_get_contents($pipes[1]);fclose($pipes[1]);proc_close($process);//在调用proc_close之前必须关闭所有管道}
2.运行linux命令:
$descriptorspec = array(0 => array("pipe", "r"),//标准输入,子进程从此管道读取数据1 => array("pipe", "w"),//标准输出,子进程向此管道写入数据2 => array("file", "/opt/figli/php/error-output.txt","a")//标准错误,写入到指定文件);$process = proc_open("ls -a", $descriptorspec, $pipes);if(is_resource($process)){echo stream_get_contents($pipes[1]);fclose($pipes[1]);proc_close($process);//在调用proc_close之前必须关闭所有管道}
运行结果:
3.阻塞运行,等待运行结果
$descriptorspec = array(0 => array("pipe", "r"),//标准输入,子进程从此管道读取数据1 => array("pipe", "w"),//标准输出,子进程向此管道写入数据2 => array("file", "/opt/figli/php/error-output.txt","a")//标准错误,写入到指定文件);$process = proc_open("php", $descriptorspec, $pipes);if(is_resource($process)){fwrite($pipes[0], "<?php sleep(5); echo 'Hello figli!';?>");fclose($pipes[0]);echo stream_get_contents($pipes[1]);fclose($pipes[1]);proc_close($process);//在调用proc_close之前必须关闭所有管道}
运行时,php将阻塞直到sleep结束后输出结果。
0 0
- PHP proc_open详解
- PHP 函数proc_open笔记
- PHP Warning: proc_open(): fork failed
- PHP中控制proc_open的执行时间
- PHP使用phantomjs导出网址的图片,并使用proc_open输出到浏览器
- PHP - Manual手册 - CXXX. Program Execution Functions程序调用函数 - proc_open执行命令并打开用于输入输出的文件指针
- proc_open(): fork failed errors
- proc_open(): fork failed errors
- proc_open(): fork failed
- 记一次proc_open没有开启心得感悟
- composer报错:proc_open(): fork failed
- 安装composer后报错proc_open(): fork failed
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- PHP配置文件详解php.ini
- [PHP]php.ini参数详解
- 细说linux IPC(三):mmap系统调用共享内存
- Apache Commons(http://commons.apache.org/)
- QT中中文路径无法识别的解决方法
- IOS产品养成记-前言
- 为你的钱找到一份好工作了吗?
- PHP proc_open详解
- Python 正则表达式提取UNC路径中的server和share
- 在HDInsight中从Hadoop的兼容BLOB存储查询大数据的分析
- 各种基金调用介绍
- file_get_contents 多参数
- 超详细mysql left join,right join,inner join用法分析
- 如何阅读别人的代码 [原]
- 黄金数据查询演示示例
- Juniper NetScreen防火墙DoS攻击的检测和防御