5

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")
Scott
  • 877
  • 4
  • 12
  • 24
  • OK, so the big deals here were 1) you need to specify the full path to sox rather than just typing "sox" and 2) the user executing the script (apache) needs to have permission to the output folder. – Scott Jan 09 '11 at 21:52

3 Answers3

4

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.

Matt S
  • 14,976
  • 6
  • 57
  • 76
  • so the fix here would be to locate the path of the program in the whole directory structure and then call that? but "normal" command line will look up the location of the program or somehow knows? Forgive me if my questions sounds silly. I'm learning linux out of necessity. – Scott Jan 09 '11 at 20:13
  • Looks like this can be done with the "which" command, I will give it a go – Scott Jan 09 '11 at 20:16
  • 1
    Each shell account on linux has a search path for typical command line apps (e.g. /usr/bin). Often the web server account's search path is empty. I wouldn't have PHP find the app, but hardcode the path somewhere (e.g. exec('/usr/local/sox/bin/sox file1... ). For my web apps I use settings files per server, and each would have the path to sox on that particular system. – Matt S Jan 09 '11 at 20:20
  • OK, Well I found the full path and it didn't solve the problem. I did copy the command into the command line with the full path and it did work. So it seems, that I have learned something about linux but that alone didn't fix the issue! – Scott Jan 09 '11 at 20:22
  • So does it look to you like I followed your advice and that was not the issue? – Scott Jan 09 '11 at 20:23
  • Well, another problem could be permissions. Does the web app have execute permissions? You'll probably want the file to be globally readable and executable. You want the "ls -l" command to show three "x" values next to sox, which means it's executable by user, group, and others. – Matt S Jan 09 '11 at 20:24
  • OK, so you were at least half right. I had your issue AND a permissions issue. If I reverted back to plain "sox" an used the "2>&1" to print errors to the screen, it said that it couldn't find "sox" but it could find the direct system path that I found from using "which". I also created a new directory with apache as the owner and it could write files to that directory. – Scott Jan 09 '11 at 21:25
2

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.

Daniel
  • 27,718
  • 20
  • 89
  • 133
  • whoami returned "apache" What is the best way to go about it, grant apache the right to use the program or authenticate as a user with more permissions using the exec command? That is if that is possible... – Scott Jan 09 '11 at 20:07
  • Use an output directory the apache can write to. Usually webapps contain a /cache/ folder for such operations – Daniel Jan 09 '11 at 20:08
  • OK, So I created a new folder called "tempfiles". I pointed the output path to this new folder with apache as the owner. The command worked if I ran it from the command line but did not from php exec. – Scott Jan 09 '11 at 20:38
  • It looks like this might actually be the solution.... I had some elements in the code that were confusing the issue.... – Scott Jan 09 '11 at 20:48
0

Check if you are currently running PHP in safe_mode

amosrivera
  • 26,114
  • 9
  • 67
  • 76