从php在计算机上运行脚本 [英] Run a script on a computer from php

查看:53
本文介绍了从php在计算机上运行脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法从php执行shell命令.目标是通过互联网(例如,使用手机)打开/关闭计算机/服务器的音乐播放器.这是我可以做的:

我有一个非常简单的文件"play.sh":代码:

  xdotool键XF86AudioPlay;回声切换"; 

如果我运行它./play.sh,那可以工作(音乐播放器打开/关闭)

然后我还有另一个非常简单的php文件"play.php":代码:

 <?php echo shell_exec("./play.sh");?> 

这两个文件位于服务器的主文件夹中,该服务器是计算机的分区.(我正在使用lampp)但是音乐正在从计算机上播放.

但是当我进入localhost/play.php时,我可以看到"switched"向我显示了sh文件已被执行,但是声音没有关闭.

我刚刚尝试了 exec() shell_exec() passthru() system(),..使用./play.sh和/opt/lampp/.../play.php 时,结果完全相同,并且没有错误消息..:/

但是,这很奇怪,我不确定,但是我认为我在计算机端运行的内容与在服务器端运行的内容不同.我的意思是,声音好像在虚拟服务器上打开/关闭,但与我的计算机的声音没有任何联系.那可能是一个解释..但是,如何从Internet/我的服务器上在我的计算机上执行脚本.?

有人有这样做的想法吗?是apache的配置吗?

谢谢!

这是我解决问题的方法:

  xhost + local:www-data 

感谢您的所有答复

解决方案

这可能是权限问题-请记住,当PHP运行命令时,它以用户身份运行 webserver www-data apache 或其他任何内容.如果您的音乐播放器是以您自己的个人用户身份运行的,那么您的脚本可能无法以其他用户身份运行时对其进行更改.

I'm unsuccessfully trying to execute a shell command from php. The goal is to switch on/off my music player of my computer/server via internet (with my phone for example). Here is what I would be able to do :

I have a very simple file "play.sh" : Code:

xdotool key XF86AudioPlay;
echo "switched";

if I run it ./play.sh, that works (music player switches on/off)

then I have an other very simple php file "play.php" : Code:

<?php echo shell_exec("./play.sh"); ?>

These two files are in the main folder of my server which is a partition of my computer. (I'm using lampp) But music is playing from my computer.

But when I'm going on localhost/play.php, I can see "switched" that showed me the sh file as been executed, but the sound doesn't turn off .

I just tried exec(), shell_exec(), passthru(), system(), .. with ./play.sh and /opt/lampp/.../play.php the result is exactly the same, and there is no error message.. :/

But that's weird, I'm not sure, but I think that what I run on my computer side is not the same than what I run on my server side. I mean that it's like the sound was turning on/off on the virtual server but had no link with the sound of my computer. That could be an explanation.. but then, how to execute a script on my computer from internet/my server..?

Does someone have an idea to do that ? Is it a configuration of apache..?

Thanks !

EDIT : Here is how I solved my problem :

xhost + local:www-data

Thanks for all your replies

解决方案

It may be a permissions issue - keep in mind that when PHP runs a command, it's run as the user the webserver is running as, e.g. www-data or apache or whatever. If your music player is running as your own personal user, your script may not have the ability to change it when run as a different user.

这篇关于从php在计算机上运行脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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