I re开发者_开发问答cently 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.
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:
<?php echo `/usr/sbin/play`; ?>
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 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.
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.
su -c /path/your/program www-data
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.
精彩评论