views:

212

answers:

4

I have a php script that handles a form input. For design reasons both a bit out of my control, and which I do not entirely wish to change, I have to call a perl script with the parameters specified in the html form.

I sanitized all inputs and then output them to a file called input, which is read by the perl script named, for sake of brevity in this question, script.pl. Script.pl should do some stuff and then write all outputs to a file named output.

I call the perl script from php like so:

system('perl script.pl 2>errors');

No good, nothing happens. output is not created, errors is not created, and the side effect does not occur.

My apache runs as www-data user and group id. My directory is set with 775 settings with ownership as me:www-data. (My user name is replaced with "me" for sakes for privacy).

My question is two fold: 1) Am I doing this wrong? If so how should I improve upon the code? 2) Is there a more sane way to catch errors in system execution?

After programming in perl for a while, php feels like a pain in the ass.

OS: Ubuntu server edition

A: 

Is perl in the path? Maybe you need to specify it fully (e.g. /usr/bin/perl). Is system() returning false, indicating a failure? If you try something simpler, like system('/usr/bin/true', $retval), does $retval get set to 1?

ysth
perl path is set, running the command manually on the terminal works. However, i just put in a check, and system call is returning false.
Razor Storm
Maybe the perl command is in the path for your interactive login/shell but not for the webserver process?
VolkerK
I specified the absolute path and it is still returning false, is there a way to check system()'s error?
Razor Storm
I believe system() will set errno, but I'm not sure if PHP accesses that. Looking into it now...
A: 

Take a look at the PHP system() documentation. The following is the function prototype of system():

string system  ( string $command  [, int &$return_var  ] )

Pass in a 2nd argument and then print out the return string as well as the second variable. See what the error says.

+1  A: 

popen can be used to get the shell's response. that is your best bet. Which can help you debug why system is angry. also, if your pl is saying "hello" and "bye", popen can even read that.

If the command to be executed could not be found, a valid resource is returned. This may seem odd, but makes sense; it allows you to access any error message returned by the shell

Ideally, I would have taken data from stdin and written to stdout. popen would allow neat access to both.

popen('pwd;perl script.pl 2>errors;echo done');

then you can see where were you (directory) when system got called and did it "done".

thevikas
Along the same lines, I've recently discovered that `proc_open` is awesomely powerful when running other processes through php, and it's not as hard to use as it may look at first.
grossvogel
A: 

In the past I have used shell_exec() or backticks to accomplish this.

The documentation for shell_exec's return value indicates it is identical to the backtick operator:

Return Values

The output from the executed command.

Hope that helps.

system() only returns the status code.

$var = shell_exec ("ls");
print $var;
$var = `ls -l`;
print $var;
Tom Dignan