php exec() 权限 - soffice 命令在终端中有效但在 php exec() 中无效
php exec() permissions - soffice command works in terminal but not in php exec()
<?php // BISMILLAHIRRAHMANIRRAHEEM
error_reporting(E_ALL);
ini_set("display_errors", 1);
// tried the following with soffice, soffice.com, soffice.exe, soffice.bin
// MS-DOS: soffice, soffice.com, soffice.bin all work successufully and generate the pdf
$ex = "soffice.com --headless --convert-to html \"" . getcwd() . DIRECTORY_SEPARATOR . "sources" . DIRECTORY_SEPARATOR . "test.docx\" --outdir \"" . getcwd() . DIRECTORY_SEPARATOR . "results\"";// 2>&1";
chdir("C:\Program Files\LibreOffice\program");
echo $ex . "<br />";
exec($ex, $output);
//exec("mkdir rr", $output); // generates directory
//exec("dir soff*", $output);
//exec("a.bat", $output); // doesn't generate pdf
print_r($output);
?>
您好,我正在使用带 Windows 7、PHP 5.6.39 的 IIS。我无法通过 PHP 调用 LibreOffice 将 docx 文档转换为 pdf。如果在 MS-DOS window 中执行,则代码示例中屏幕的命令输出有效,但在 php 的 exec() 或 shell_exec() 中无效。该文件夹似乎具有适当的权限,因为我的上传脚本和 exec("mkdir newdir") 正在运行。
exec("dir");
还会输出上述路径的目录列表,因此 DOS 导航命令似乎也能正常工作。
我想 成为另一个用户。不优雅。这应该不难 INSHAALLAH。
不工作,我的意思是没有生成文档,exec()
也没有生成输出/错误,这使得调试变得困难。
感谢您的宝贵时间。
- 更新:
经过一些摆弄,我意识到 return 代码是:-1073741515
Google 结果表明它显然是文件 I/O 失败。
我下载的另一个执行类似操作的脚本也给出了同样的错误。所以要查原因。正如我之前所说,可以创建目录和上传文件,因为它有权限。
在我发布的link中,似乎需要创建新用户。是否有可能 IIS 对该目录有权限但对 PHP exec()
?
没有权限
- 在 Linux 上,它给我错误代码
77
,权限被拒绝?
BISMILLAHIRRAHMANIRRAHEEM
ALHAMDOLILLAH 经过多次搜索,终于找到了答案。
Windows:
我之前做了一些权限等,对于 IIS 应该是微不足道的。但是最终解决问题的是this.
显然需要指定 -env:UserInstallation=file:///C:\some\path
,因为如果不指定不同的配置文件文件夹 (Reference),您不能同时 运行 多个 LibreOffice 实例。
另外,请参阅相同的解决方案 here。
在 IIS 上通过 PHP 执行程序时,Windows 的用户帐户是 IUSR 帐户。
所以,
- 给 IUSR 写(和修改?我给完全)权限,以便能够写入
C:\inetpub\wwwroot\path
目录。
- 将目录更改为
soffice.com
的 LibreOffice 路径(如果在执行时提供完整路径则不需要):
chdir("C:\Program Files\LibreOffice\program");
- 设置 HOME 环境变量,因为 LibreOffice 需要一个可写的临时路径。 IUSR 必须有权访问此路径。我只是重复使用
wwwroot
(这一步可以是可选的):
putenv("HOME=C:\inetpub\wwwroot\temp");
- 在 PHP 中执行,类似于:
exec("\"C:\Program Files\LibreOffice\program\soffice.com\" --headless \"-env:UserInstallation=file:///C:/inetpub/wwwroot/LibreOfficeProfilePath1\" --convert-to pdf:writer_pdf_Export --outdir \"C:\inetpub\wwwroot\result\" \"C:\inetpub\wwwroot\source\file.docx\"", $output, $ret);
// Displaying the command output
print_r($output);
echo "<br />\n\n<br />"; // Line breaks for view-source
echo "Return code: " . $ret;
- 通过删除 LibreOffice 的配置文件目录进行清理。由于您要在服务器环境中为 LibreOffice 的并发会话即时创建唯一目录,因此您也想删除它们。以下片段来自 here:
exec("rmdir /S /Q \"C:\inetpub\wwwroot\LibreOfficeProfilePath1\"");
您可以查看自动生成此配置文件路径,例如:
// Do this before executing soffice command and pass it to -env
$profilepath = "C:/inetpub/wwwroot/LibreOfficeProfilePath" . date("YmdHis") . rand(0, 999999);
// Make sure to replace '/' with '\' when deleting
exec("rmdir /S /Q \"" . str_replace("/", "\", $profilepath) . "\""); // Windows
exec("rm -rf \"" . str_replace("/", "\", $profilepath) . "\"); // Linux
或者使用 PHP 删除包含文件的目录,如图 here.
LibreOffice 运行作为服务 PHP 请求文档转换可能是更好的解决方案,但这 post 涉及每次需要文档转换时调用 LibreOffice,然后让它关闭。也可以使用单独的处理方法来避免等待 LibreOffice 完成转换。
Linux:
www-data
的类似步骤,因为那是 Linux 中的 apache 用户帐户。查看解决方案 here.
- www-data没有$HOME目录,需要按照上面(3)的方式指定。
env
路径类似于:
\"-env:UserInstallation=file:///var/www/tmp\"
请注意 file:
之后的三个 /
,无论 Windows 还是 Linux,即使 Linux 中的路径以 /
开头,或者也许他们忽略了 Windows 中的斜杠,不过对我来说似乎不一致。
INSHAALLAH 之后就可以了。
<?php // BISMILLAHIRRAHMANIRRAHEEM
error_reporting(E_ALL);
ini_set("display_errors", 1);
// tried the following with soffice, soffice.com, soffice.exe, soffice.bin
// MS-DOS: soffice, soffice.com, soffice.bin all work successufully and generate the pdf
$ex = "soffice.com --headless --convert-to html \"" . getcwd() . DIRECTORY_SEPARATOR . "sources" . DIRECTORY_SEPARATOR . "test.docx\" --outdir \"" . getcwd() . DIRECTORY_SEPARATOR . "results\"";// 2>&1";
chdir("C:\Program Files\LibreOffice\program");
echo $ex . "<br />";
exec($ex, $output);
//exec("mkdir rr", $output); // generates directory
//exec("dir soff*", $output);
//exec("a.bat", $output); // doesn't generate pdf
print_r($output);
?>
您好,我正在使用带 Windows 7、PHP 5.6.39 的 IIS。我无法通过 PHP 调用 LibreOffice 将 docx 文档转换为 pdf。如果在 MS-DOS window 中执行,则代码示例中屏幕的命令输出有效,但在 php 的 exec() 或 shell_exec() 中无效。该文件夹似乎具有适当的权限,因为我的上传脚本和 exec("mkdir newdir") 正在运行。
exec("dir");
还会输出上述路径的目录列表,因此 DOS 导航命令似乎也能正常工作。
我想
不工作,我的意思是没有生成文档,exec()
也没有生成输出/错误,这使得调试变得困难。
感谢您的宝贵时间。
- 更新: 经过一些摆弄,我意识到 return 代码是:-1073741515 Google 结果表明它显然是文件 I/O 失败。 我下载的另一个执行类似操作的脚本也给出了同样的错误。所以要查原因。正如我之前所说,可以创建目录和上传文件,因为它有权限。
在我发布的link中,似乎需要创建新用户。是否有可能 IIS 对该目录有权限但对 PHP exec()
?
- 在 Linux 上,它给我错误代码
77
,权限被拒绝?
BISMILLAHIRRAHMANIRRAHEEM
ALHAMDOLILLAH 经过多次搜索,终于找到了答案。
Windows:
我之前做了一些权限等,对于 IIS 应该是微不足道的。但是最终解决问题的是this.
显然需要指定 -env:UserInstallation=file:///C:\some\path
,因为如果不指定不同的配置文件文件夹 (Reference),您不能同时 运行 多个 LibreOffice 实例。
另外,请参阅相同的解决方案 here。
在 IIS 上通过 PHP 执行程序时,Windows 的用户帐户是 IUSR 帐户。
所以,
- 给 IUSR 写(和修改?我给完全)权限,以便能够写入
C:\inetpub\wwwroot\path
目录。 - 将目录更改为
soffice.com
的 LibreOffice 路径(如果在执行时提供完整路径则不需要):
chdir("C:\Program Files\LibreOffice\program");
- 设置 HOME 环境变量,因为 LibreOffice 需要一个可写的临时路径。 IUSR 必须有权访问此路径。我只是重复使用
wwwroot
(这一步可以是可选的):
putenv("HOME=C:\inetpub\wwwroot\temp");
- 在 PHP 中执行,类似于:
exec("\"C:\Program Files\LibreOffice\program\soffice.com\" --headless \"-env:UserInstallation=file:///C:/inetpub/wwwroot/LibreOfficeProfilePath1\" --convert-to pdf:writer_pdf_Export --outdir \"C:\inetpub\wwwroot\result\" \"C:\inetpub\wwwroot\source\file.docx\"", $output, $ret);
// Displaying the command output
print_r($output);
echo "<br />\n\n<br />"; // Line breaks for view-source
echo "Return code: " . $ret;
- 通过删除 LibreOffice 的配置文件目录进行清理。由于您要在服务器环境中为 LibreOffice 的并发会话即时创建唯一目录,因此您也想删除它们。以下片段来自 here:
exec("rmdir /S /Q \"C:\inetpub\wwwroot\LibreOfficeProfilePath1\"");
您可以查看自动生成此配置文件路径,例如:
// Do this before executing soffice command and pass it to -env
$profilepath = "C:/inetpub/wwwroot/LibreOfficeProfilePath" . date("YmdHis") . rand(0, 999999);
// Make sure to replace '/' with '\' when deleting
exec("rmdir /S /Q \"" . str_replace("/", "\", $profilepath) . "\""); // Windows
exec("rm -rf \"" . str_replace("/", "\", $profilepath) . "\"); // Linux
或者使用 PHP 删除包含文件的目录,如图 here.
LibreOffice 运行作为服务 PHP 请求文档转换可能是更好的解决方案,但这 post 涉及每次需要文档转换时调用 LibreOffice,然后让它关闭。也可以使用单独的处理方法来避免等待 LibreOffice 完成转换。
Linux:
www-data
的类似步骤,因为那是 Linux 中的 apache 用户帐户。查看解决方案 here.- www-data没有$HOME目录,需要按照上面(3)的方式指定。
env
路径类似于:
\"-env:UserInstallation=file:///var/www/tmp\"
请注意 file:
之后的三个 /
,无论 Windows 还是 Linux,即使 Linux 中的路径以 /
开头,或者也许他们忽略了 Windows 中的斜杠,不过对我来说似乎不一致。
INSHAALLAH 之后就可以了。