函数名称:popen() 适用版本:所有版本 用法:popen()函数用于打开一个管道,可以通过该管道执行一个外部程序,并返回一个文件指针。该文件指针可以用于读取外部程序的输出或者向外部程序发送输入。...
函数名称:popen()
适用版本:所有版本
用法:popen()函数用于打开一个管道,可以通过该管道执行一个外部程序,并返回一个文件指针。该文件指针可以用于读取外部程序的输出或者向外部程序发送输入。
语法:resource popen ( string $command , string $mode )
参数:
返回值:如果成功,则返回一个文件指针,否则返回 false。
示例:
// 执行外部程序,并读取输出
$handle = popen('ls', 'r');
if ($handle) {
while (!feof($handle)) {
echo fgets($handle);
}
pclose($handle);
}
// 执行外部程序,并向其发送输入
$handle = popen('grep "example"', 'w');
if ($handle) {
fwrite($handle, "This is an example.\n");
pclose($handle);
}
注意事项: