Executing linux commands with PHP(用 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) . "
";
$read = fread($handle, 2096);
echo $read;
pclose($handle);
?>
这篇关于用 PHP 执行 linux 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:用 PHP 执行 linux 命令
基础教程推荐
- 如何在 Laravel 中使用 React Router? 2022-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
