1

I currently have an Apache web server running on my Pi that has this as the index.php file:

<?php
    if (isset($_GET['play'])) {
        exec('sudo omxplayer yes.m4a');
    }
?>

However, my code doesn't work! When I visit the page, nothing happens on my Pi. I know that exec does work, because when I use another command such as li, I get an output. Is there any error(s) in my code?

Whenever someone visits the webpage with the url, say, 10.0.1.23/?play=1, I want the Pi to run my audio file. I have been looking for what feels like forever, but to no avail.

If this is not possible for any reason (although I am almost sure it is), then what would be the easiest way to do this? I need to have my audio file get executed from another computer with minimal delay, as this is for my lab where, as long as it's consistent, a 1/4 of a second delay would be fine.

Edit: I have tried running the web server on my mac with the same code, only instead of sudo omxplayer yes.m4a I used say test and it seemed to have worked fine. Still not sure why my Pi isn't running my audio.

12
  • Does sound work correctly otherwise (without PHP)? – Steve Robillard Dec 20 '14 at 4:54
  • @SteveRobillard Yes. Not sure why it's not working when running it via PHP. – Andrew Dassonville Dec 20 '14 at 4:56
  • Where are you expecting the sound to play on the computer with the webpage or back to the client over the web? – Steve Robillard Dec 20 '14 at 5:16
  • @SteveRobillard I am expecting the sound to play on my Pi. – Andrew Dassonville Dec 20 '14 at 5:21
  • I think the problem is the sudo. First the sudo is going to try and run as the webserver user which may not be sudo enabled. running exec can be a security problem and this adds to that insecurity. One way around this may be to create a bash script to run the audio file and call that instead of the omxplayer command. The bash script will need to have the owner/group permissions set so the webserver user can call it. Another alternative is to add an entry to the sudoers file (make sure to edit this file with sudo visudo this checks for syntax errors which could stop your system from booting). – Steve Robillard Dec 20 '14 at 8:21
2

you have to specify the full path to the executable and/or data files:

exec('/usr/bin/sudo /check/the/path/omxplayer /your/home/directory/or/whatever/yes.m4a');

you may find the path to omxplayer using command which omxplayer and path to your data file using your linux command line experience.

2
  • Thanks for the help. I just tried using exec('/usr/bin/sudo /usr/bin/omxplayer /var/www/yes.m4a'); and it still doesn't work in PHP. However, it does work in command line, so could this have anything to do with where the audio gets output from when being run from PHP? Sort of a long shot, but I'm very new to Raspberry Pi programming. I also now know that exec its self does work, because I tried making the page run mkdir and it successfully created the directory. – Andrew Dassonville Dec 20 '14 at 4:44
  • @AndrewDassonville when you run sudo you might want to provide your password in some form, use man sudo to seek for your options. and I hope your web server runs under your name, because for user www or www-data there might be no access to sudo or the password is totally different. – lenik Dec 20 '14 at 12:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.