Unoconv works from terminal using www-data but not from php script also as www-data

I wrote the following function in php

public static function convert($originFilePath, $outputDirPath, $toFormat)
{
    $command = 'echo $PATH & UNO_PATH=/usr/lib/libreoffice unoconv --format %s --output %s %s';
    $command = sprintf($command, $toFormat, $outputDirPath, $originFilePath);
    exec($command, $output, $result_var);

    return compact('output', 'result_var', 'outputDirPath', 'originFilePath', 'toFormat');
}

It did not generate any error message, or any pdf file as well.

In terminal, when I run the unoconv directly as www-data, I had no issues.

This is my result after execution:

2013-05-26 03:05:30 Error: Array
(
    [output] => Array
        (
            [0] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
        )

    [result_var] => 1
    [outputDirPath] => /var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/2
    [originFilePath] => /var/virtual/storyzer.com/cake-json/ltequotationapp/webroot/outputfiles/Excel/2/dsadas.xlsx
    [toFormat] => pdf
)

Please advise.

Do you use full paths? Do they contain unicode characters (non-ASCII ones)? What happens when you run your script from terminal as www-data?

this script is part of a framework (cakephp) so it is hard to run it as a standalone script. however, I have no problems running the php -r “exec(unoconv … )” so I am not sure why this is the case. I am using full paths.

@oweng my $PATH is /usr/local/sbin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/node/bin:/opt/node/bin

Is it possible to run your cakephp script as www-data to make sure it’s not permissions problem?

I have done so, it is not permissions problem

The issue is that I am using Nginx and PHP-FPM.

This means that the PATH is not explicitly declared.

hence causing the issue.

For more details, read http://stackoverflow.com/a/16889381/80353

Well done on finding a solution.

THis is annoying. I cannot accept my own answer because my points less than 50. And I am continuously prompted to accept an answer for this question. What can I do to circumvent this?

I have given you a bit more karma, but it is a site restriction. Does the information in this question help with the messages?

I still have less than 50 karma. Not enough to mark as correct. Somebody please help to mark as correct. This is annoying.

Others have recently been marking their answers as correct with less karma, so give it a go and see what happens. There appears to have been a change of policy recently.

What is your $PATH? This Stack Overflow thread suggests “prepending /usr/local/sbin to the PATH environment variable.”