如何从PHP执行交互式命令? [英] How to execute an interactive command from PHP?

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

问题描述

我需要从 PHP 脚本(使用浏览器,而不是命令行)在我的台式机 (localhost) 中执行 kdiff3 命令.我已授予用户 www-data 的权限,该用户正在执行脚本以使用 visudo 执行kdiff3.实际上,如果我以 www-data 的身份登录,则可以执行它而不会出现问题( sudo kdiff3 ... ,它被配置为根本不要求输入密码).

I need to execute kdiff3 command in my desktop machine (localhost) from PHP script (using browser, not command line). I've given permission for the user www-data that is executing the scripts to execute kdiff3 using visudo. In fact, if I log in as www-data I can execute it without problems (sudo kdiff3 ..., it's configured to not ask for a password at all).

问题是当我尝试从PHP脚本执行此命令时.我已经尝试过了:

The problem is when I try to execute this command from a PHP script. I've tried this:

$output = shell_exec("sudo kdiff3 -m $file.def.old $file $file.def -o $file");

,没有任何反应(输出为NULL).如果我尝试使用非交互式命令,例如 ls ,那么它会起作用:

and nothing happens (output is NULL). If I try a non interactive command, like ls it works:

$output = shell_exec("ls");

发生了什么事?为什么不能执行交互式命令?

What's happening? Why cannot execute an interactive command?

推荐答案

kdiff3 是交互式图形用户界面程序,因此它使用KDE和Qt,并且需要 X11 服务器.

kdiff3 is an interactive graphical user interface program, so it use KDE and Qt and requires an X11 server.

在网络服务器(即在运行 Apache Lighttpd PHP 中)中,您没有任何X11服务器.

And within a web server (i.e. in PHP running for Apache or Lighttpd) you don't have any X11 server.

因此,无法从PHP脚本内部使用 kdiff3 (除非Web服务器在台式机Linux计算机上运行;然后需要适当设置环境,尤其是DISPLAY 和可能的 XAUTHORITY 环境变量).但是,您可以运行诸如 diff3 ...之类的命令行程序(使用 popen 技巧).

So there is no way to use kdiff3 from inside a PHP script (unless the web server is running on your desktop Linux machine; and then you need to set appropriately the environment, notably DISPLAY and probably XAUTHORITY environment variable). However you could run a command line program like diff3 ... (using the popen tricks).

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

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