Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any idea why I can execute command from Command Line but not From PHP exec()

OK,

I have done some creative searches and am kind of hitting a road block.

I am trying to use the linux program "sox." I am trying to call it from my PHP script. The script DOES work if I use the command line. However, when I use PHP exec, it does not work.

Example:

sox file1.mp3 file2.mp3 tempfile.mp3 -V3

("V3" specifies a verbose output)

When executing in the command line as "User X" or as root, I am able to create the new file. However, when I execute the command as:

<?php
exec('sox file1.mp3 file2.mp3 tempfile.mp3  -V3', $output);
foreach($output as $line){
print $line;
}

It does not generate the new file. Furthermore, the array that should return the results of of the command is blank.

I have done a simple text with

exec(ls,$output);

and i get the contents of the root directory.

I used the PHP command get_current_user() and it returned the owner of the directory that is the root of the web site.

However, when I use the linux command whoami I found out that it views the current person as "apache." Would I need to give apache the rights to use the program?

I will continue to search online and do trial and error in an effort to figure out what I am doing wrong. Any help is appreciated.

Clarifications

  • I am not in safe mode (I checked the phpinfo() page")
like image 583
Scott Avatar asked Jan 09 '11 20:01

Scott


2 Answers

Typically this is a path problem. The application you're trying to call is probably not in the search path of the web server's user account. I only call applications from PHP using the full path. I store that path in settings files so it works across any system it's used on.

like image 177
Matt S Avatar answered Sep 27 '22 23:09

Matt S


I am sure php runs as the user running your apache, and this is most often nobody (a user without rights to create directories). Try to create your output somewhere where everyone is allowed to write and check the user apache runs under.

like image 24
Daniel Avatar answered Sep 27 '22 23:09

Daniel