PHP how to execute command

I am trying to use LibreOffice to convert a table to a different format, when I execute a command from the console it works fine, but when I do it from PHP using exec () or system () it doesn't work. It doesn't show any error or whatever, it just silently fails, if I try to execute some simple command like "ls" it works fine.

This is the command I'm using:

<?php
system("libreoffice --headless -convert-to ooxml '/home/www/path_to_app/file.xlsx' -outdir /home/www/path_to_app/");

      

I've already tried changing apache User and Group to /opt/lampp/etc/httpd.conf to the same logged in user.

I am wondering if the problem is the www / home folder, not inside my user, and which is causing permissions issues but still couldn't get it to work.

Any help would be appreciated.

+3


source to share


4 answers


Despite your $ PATH referenced by 01010, I wouldn't do that.

LibreOffice is a fairly large program, it has a lot of code that you don't know about, it generates and updates files in a directory in your $ HOME, and of course you can't run more than one instance at a time.

So, instead of having LibreOffice run by your web server and undermine Apache's security by running it as a more privileged user than "www-data" or "nobody", you should make a handler.

First of all, make sure that you can run the command line libreoffice ...

from the terminal. To make sure you don't have X11 dependencies, run unset DISPLAY

(for bash) or unsetenv DISPLAY

(for tcsh) in an xterm before you check your command line. Does it rip? Please fix this problem first. It works? Great, then go to the handler.

Your handler in its simplest form could be a script that forever checks the "files to process" in the spool directory forever, and if it finds them, converts them with libreoffice and puts the resulting file where it can be found in the web application.

#!/bin/sh

while sleep 10; do
  if [ `ls /var/tmp/myspool/ | grep -c '\.xlsx$'` -gt 0 ]; then
    ls /var/tmp/myspool/*.xlsx | while read file; do
      /usr/local/bin/libreoffice --headless -convert-to ooxml "$file" yadda yadda
      if [ $? = 0 ]; then
        mv "$file" "/var/tmp/myspool/done/
      fi
    done
  fi
done

      



If you don't need the overhead of "polling" anything (checking the spool directory every 10 seconds), you can add your PHP script line to a log that will be tracked by your handler. For example:

<?php

// process form, save file to spool dir
syslog(LOG_NOTICE, "Saved: " . $filename);

?>

      

Make sure you set up syslog to store these messages, say /var/log/filelog

, then your handler can just close the log.

#!/bin/sh

tail -F /var/log/filelog | while read line; do
  filename="`echo \"$line\" | sed 's/.*Saved: //'`"
  /usr/local/bin/libreoffice --headless -convert-to ooxml "$file" yadda yadda
  # etc ... error handling and mv as in the other script
done

      

Get an idea?

+3


source


I solved this problem with the following command:



system('
  export HOME=/tmp 
  libreoffice --headless --invisible --norestore --convert-to pdf --outdir /my/folder /my/file/calc.xls');

      

+5


source


I solved this problem with the following command:

exec('libreoffice --headless --invisible --norestore --convert-to pdf --outdir /my/folder /my/file/calc.xls');

      

Just check the permissions, libreoffice is using the user's home folder. If you are using Ubuntu default settings then PHP runs heren www-data rights eenter code and home is / var / www / but by default this directory belongs to root.enter code here

In PHP - you can see the rigths:

exec('whoami', $arr);
print_r($arr);

      

In the console - you can see your HOME and give the correct rights:

su wwww-data
echo $HOME
exit;
chown www-data:www-data /var/www -R

      

+4


source


I also faced the same problem before .....

This solution works for me ...

function execInBackground($cmd) { 
    if (substr(php_uname(), 0, 7) == "Windows"){ 
        pclose(popen("start /B ". $cmd, "r"));  
    } 
    else { 
        exec($cmd . " > /dev/null &");   
    } 
} 

$mainFile = "YOUR FILE";     
$cmd2 = "export HOME=/tmp
             libreoffice --headless --invisible --norestore --convert-to pdf " .
                    "'$mainFile'";



        $saved = getenv("LD_LIBRARY_PATH");        // save old value 
        $newld = "/usr/lib";  // extra paths to add 
        if ($saved) {

            putenv("LD_LIBRARY_PATH=" . $newld);
        }           
                // set new value 
        //


        // command is loaded using 
        // libs in the new path list 
        execInBackground($cmd2);
        putenv("LD_LIBRARY_PATH=$saved");        // restore old value 

      

0


source







All Articles