从 php 在计算机上运行脚本

发布于 2024-12-29 19:26:37 字数 922 浏览 1 评论 0原文

我尝试从 php 执行 shell 命令失败。目标是通过互联网(例如我的手机)打开/关闭我的计算机/服务器的音乐播放器。这是我能够做的:

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

xdotool key XF86AudioPlay;
echo "switched";

如果我运行它./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 结果完全相同,并且没有错误消息.. :/

但这很奇怪,我不是当然,但我认为我在计算机端运行的是与我在服务器端运行的不一样。我的意思是,这就像声音在虚拟服务器上打开/关闭,但与我的计算机的声音没有链接。这可能是一个解释..但是,如何从互联网/我的服务器在我的计算机上执行脚本..?

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

谢谢 !

编辑 : 这是我解决问题的方法:

xhost + local:www-data

感谢您的所有回复

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事灯 2025-01-05 19:26:37

这可能是一个权限问题 - 请记住,当 PHP 运行命令时,它是以 webserver 运行的用户身份运行的,例如 www-dataapache 或其他。如果您的音乐播放器以您自己的个人用户身份运行,则您的脚本在以其他用户身份运行时可能无法更改它。

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.

你在我安 2025-01-05 19:26:37

Shell_exec 有效。尝试将 .sh 脚本放入可执行文件的绝对路径

/usr/bin/xdotool key XF86AudioPlay;

Shell_exec works. Try to put in your .sh script absolute path to the executables

/usr/bin/xdotool key XF86AudioPlay;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文