PHP how to execute a command

前端 未结 4 1663
北荒
北荒 2021-01-06 13:49

I\'m trying to use LibreOffice for converting a spreadsheet to another format, when I execute the command from the console it works fine but when I do it from PHP using exec

4条回答
  •  忘掉有多难
    2021-01-06 14:14

    I also faced same problem before.....

    This solution seems worked 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 
    

提交回复
热议问题