用PHP执行Linux命令 [英] Executing linux commands with PHP

查看:127
本文介绍了用PHP执行Linux命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过PHP命令行脚本执行linux命令,使用exec命令没问题.

I'm trying to execute a linux command through a PHP command-line script, which is no problem using the exec command.

问题是,如果发生错误(例如,用户/密码不正确),我正在执行的命令(mysqldump)将输出一条错误消息.我似乎无法捕获此错误以进行记录.它将这个错误打印到屏幕上.

The problem is, the command I am executing (mysqldump) outputs an error message if something is wrong (for example user/password is incorrect). I can't seem to be able to capture this error in order to log it. It just prints this error to the screen.

如何使此错误不显示在屏幕上,而是将其放在变量中以供脚本使用?

How do I cause this error not to be printed to the screen, but instead to put it in a variable for use in my script?

谢谢!

推荐答案

使用 popen 来运行该过程.此页面上的示例2准确显示了您要查找的内容:

Use popen to run the process. The example #2 on this page shows exactly what you're looking for:

<?php
error_reporting(E_ALL);

/* Add redirection so we can get stderr. */
$handle = popen('/path/to/spooge 2>&1', 'r');
echo "'$handle'; " . gettype($handle) . "\n";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
?>

这篇关于用PHP执行Linux命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆