简体   繁体   English

以PHP用户身份执行Linux Linux命令

[英]Exec Linux command as user in PHP

I'm converting a document file to html. 我正在将文档文件转换为html。 I've installed LibreOffice on Linux server. 我已经在Linux服务器上安装了LibreOffice。

Here's the command, 这是命令

$cmd = "libreoffice4.1 --headless -convert-to html /var/www/html/CV.doc -outdir /var/www/html/";

This command is working fine when I execute directly on the Linux server but when I tried to execute it via PHP it will not convert. 当我直接在Linux服务器上执行该命令时,该命令运行良好,但是当我尝试通过PHP执行该命令时,该命令将不会转换。

exec($cmd,$out,$err);

The $err is printing 127 . $err正在打印127

Kindly, let me know what I'm doing wrong here. 请让我知道我在做什么错。

The most common reason for this is that the $PATH environment variable hasn't been populated to your PHP application. 最常见的原因是$PATH环境变量尚未填充到您的PHP应用程序中。 In this case use the full path to libreoffice: 在这种情况下,请使用libreoffice的完整路径:

$cmd = "/usr/bin/libreoffice4.1 --headless  ... ";

The path /usr/bin may differ on your system. 路径/usr/bin在您的系统上可能有所不同。 Type which libreoffice4.1 in terminal to find it out. 在终端中键入which libreoffice4.1可以找到它。


Another reason for the problem might be insufficient permissions to write files into the target directory or read the original documents. 问题的另一个原因可能是权限不足,无法将文件写入目标目录或读取原始文档。 In this case make sure that the php application has proper permissions to do so. 在这种情况下,请确保php应用程序具有适当的权限。 ! root permissions must not being required for that! 绝对不需要 root权限! This would be a security flaw in your application. 这将是您应用程序中的一个安全漏洞。

You say this: 你这样说:

This command is working fine when I execute directly on the Linux server but when I tried to execute it via PHP it will not convert. 当我直接在Linux服务器上执行该命令时,该命令运行良好,但是当我尝试通过PHP执行该命令时,该命令将不会转换。

And your command is this: 您的命令是这样的:

$cmd = "libreoffice4.1 --headless -convert-to html /var/www/html/CV.doc -outdir /var/www/html/";

It could be that as a user logged into the system the path to libreoffice4.1 is part of your default user path. 可能是因为当用户登录系统时, libreoffice4.1的路径是默认用户路径的一部分。 But when Apache (I assume) is running PHP it does not know where the libreoffice4.1 binary is. 但是当Apache(我假设)正在运行PHP时,它不知道libreoffice4.1二进制文件在哪里。

So my solution would be to include the full path to the file in your command like this: 所以我的解决方案是在命令中包括文件的完整路径,如下所示:

$cmd = "/full/path/to/this/binary/libreoffice4.1 --headless -convert-to html /var/www/html/CV.doc -outdir /var/www/html/";

Of course replacing /full/path/to/this/binary/ with the actual path to the file. 当然,将/full/path/to/this/binary/替换为文件的实际路径。

You can easily determine the full path to the binary by logging into your sever & typing in the following command: 您可以登录服务器并输入以下命令,轻松确定二进制文件的完整路径:

which libreoffice4.1

The returned output should be the full path your libreoffice4.1 binary file. 返回的输出应该是libreoffice4.1二进制文件的完整路径。 The path should be something like this: 路径应该是这样的:

/usr/bin/libreoffice4.1

Or this: 或这个:

/usr/local/bin/libreoffice4.1

But the which command on your system will return the full path to the file, so use that output as your guide. 但是,系统上的which命令将返回文件的完整路径,因此请使用该输出作为指导。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM