在 Linux 环境中从 PHP 脚本运行应用程序时出现问题
我最近写了一个C程序,应该在Linux环境中运行,如下所示;
#include <stdio.h>
#include <stdlib.h>
void main()
{
system("notify-send -u normal -t 200 'You Have received a new message'");
system("cvlc /home/ashenafis/Music/BabyMessage.mp3");
return 0;
}
编译它并将可执行文件保存在“/usr/sbin/play”中。
”从 PHP 脚本运行它时,它不起作用
<?php exec("/usr/sbin/play"); ?>
当我从终端运行它时,它工作正常,但是当我尝试使用“是否有我遗漏的东西? ?请帮忙。
I recently wrote a C program that is supposed to run in a Linux environment as follows;
#include <stdio.h>
#include <stdlib.h>
void main()
{
system("notify-send -u normal -t 200 'You Have received a new message'");
system("cvlc /home/ashenafis/Music/BabyMessage.mp3");
return 0;
}
Compiled it and saved the executable in "/usr/sbin/play".
When I run it from the terminal it works fine, however it does not work when I try to run it from a PHP script using
<?php exec("/usr/sbin/play"); ?>
Is there something I am missing? Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试使用 反引号运算符,它执行一个 shell然后调用程序,而不是直接调用程序。代码如下:
You could try using the backtick operator, which executes a shell which then calls the program, instead of calling the program directly. The code would be this:
我不知道这是否多余,但你可以尝试 SWIG,它是:一种软件开发工具,它将用 C 和 C++ 编写的程序与各种高级编程语言连接起来。
您可以在此处查看支持的语言 http://www.swig.org/compat.html#SupportedLanguages ,表明支持 PHP。我想这就是您正在寻找的。
I do not know this is an overkill but you can try SWIG, which is: a software development tool that connects programs written in C and C++ with a variety of high-level programming languages.
You can see from the supportted languages here http://www.swig.org/compat.html#SupportedLanguages, that PHP is supported. I think this is what you are looking for.
notify-send 和 cvlc 可能不在 PHP 已知的路径中。尝试让 C 程序通过完整文件名来引用它们。另外,确保相关的东西有o+x权限。
notify-send and cvlc might not be in the path known to PHP. Try making the C program refer to them by their full filenames. Also, make sure the relevant things have o+x permission.
请记住,这将尝试以 Web 服务器运行的任何用户身份运行,这很可能出于安全原因而受到限制。例如,在我使用 Apache 的机器 (Ubuntu) 上,有一个名为
www-data
的用户。您可以使用“su”命令尝试以该用户身份运行您的程序,并可能查看问题所在。如果您不知道该用户的密码,请以 root 身份运行 su 命令。想想看,我非常怀疑 Web 服务器用户是否有权访问“系统”二进制文件,即
/usr/sbin
中的任何内容。在/var/www/cgi-bin
或类似的地方可能会更好。Keep in mind this will be trying to run as whatever user the web server is running as, which most likely is restricted for security reasons. For example on my machine (Ubuntu) using Apache it is a user called
www-data
. You can use the 'su' command to try running your program as this user, and possibly see what the problem is. If you don't know the password for this user, run the su command as root.Come to think of it, I very much doubt the web server user has access to the 'system' binaries i.e. anything in
/usr/sbin
. It is possibly better in/var/www/cgi-bin
or something like that.