PHP如何执行命令 [英] PHP how to execute a command

查看:141
本文介绍了PHP如何执行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用的LibreOffice作为为preadsheet转换为另一种格式,当我从它工作正常,但是当我使用EXEC()或系统(从PHP这样做)没有关系控制台执行命令将不起作用。
它不显示任何错误或任何东西,它只是静静地失败,如果我尝试执行如ls一些简单的命令,它工作得很好。

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() or system() it doesn't work. It doesn't show any error or anything, it simply silently fails, if I try executing some simple command like "ls" it works just fine.

这是我使用的命令:

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

我已经试图改变对/opt/lampp/etc/httpd.conf apache的用户和组,以登录用户相同的我。

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

我想知道如果问题是www文件夹为/ home,而不是我的用户里面,导致权限问题,但至今未能使它工作。

I'm wondering if the problem is that the www folder is /home instead of inside my user and that causes permissions problems, but so far couldn't make it work.

任何帮助将AP preciated。

Any help will be appreciated.

推荐答案

尽管你的$ PATH,这drew010提到,我不会做这种方式。

Notwithstanding your $PATH, which drew010 alluded to, I wouldn't do it this way.

的LibreOffice是pretty大项目,有很多code,你不知道,它会生成和更新文件在$ HOME目录,肯定是不是你要的能够同时运行的多个副本。

LibreOffice is a pretty big program, has lots of code you don't know about, it generates and updates files in a directory in your $HOME, and is certainly not something you're going to be able to run more than one copy of at a time.

因此​​而不必通过的LibreOffice Web服务器发起,并通过运行它作为比WWW的数据或无人更特权用户颠覆Apache的安全性,你应该做的处理程序。

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

首先,请确认您可以运行从终端的的LibreOffice ... 命令行。要特别确保你没有任何X11的依赖,运行取消设置DISPLAY (对于bash)或 unsetenv显示 (对于tcsh)在你的xte​​rm您测试您的命令行之前。它打破?首先解决这个问题。它的工作原理?太好了,然后用处理程序进行。

First off, verify that you can run your libreoffice ... command line from a terminal. To be extra sure that you don't have any X11 dependencies, run unset DISPLAY (for bash) or unsetenv DISPLAY (for tcsh) in your xterm before you test your command line. Does it break? Fix that problem first. Does it work? Great, then proceed with a handler.

您处理程序,在它的最简单的形式,可以是循环永远,检查文件来处理,在假脱机目录的脚本,并且如果它发现他们使用的LibreOffice转换它们并将所得文件,其中它可以是您的Web应用程序中。

Your handler, in its simplest form, can be a script that loops forever, checking for "files to handle" in a spool directory, and if it finds them, converts them using libreoffice and puts the resultant file where it can be found by your web app.

#!/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

如果你不想要的东西轮询(检查后台打印目录每10秒)的开销,那么你可以有你的PHP脚本中添加一行,获取你的处理器看了日志。例如:

If you don't want the overhead of something "polling" (checking the spool directory every 10 seconds), then you could have your PHP script add a line to a log that gets watched by your handler. For example:

<?php

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

?>

请确保你已经配置的系统日志存储在,比方说,这些消息在/ var /日志/ filelog ,那么你的处理程序可以只是尾部的日志。

Make sure you've configured syslog to store these messages in, say, /var/log/filelog, then your handler can just tail 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

有此想法?

这篇关于PHP如何执行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆