[英]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 窗口中执行,代码示例中屏幕的命令输出有效,但在 php 的 exec() 或 shell_exec() 中无效。 该文件夹似乎具有适当的权限,因为我的上传脚本和 exec("mkdir newdir") 正在工作。 exec("dir");
还输出上述路径的目录列表,因此 DOS 导航命令似乎也能正常工作。
我想避免使用其他用户的解决方案。 不优雅。 这应该不难 INSHAALLAH。
不工作,我的意思是没有生成文档,也没有exec()
生成输出/错误,这使得调试变得困难。
感谢您的时间。
在我发布的链接中,似乎需要创建新用户。 IIS 是否有权限访问该目录但没有 PHP exec()
权限?
77
,权限被拒绝?比斯米拉希拉赫曼尼拉希姆
ALHAMDOLILLAH 经过一番搜索,终于找到了答案。
我之前做了一些权限等,对 IIS 应该是微不足道的。 但最终解决了这个问题的是这个。
显然需要指定-env:UserInstallation=file:///C:\\some\\path
因为如果不指定不同的配置文件文件夹( 参考),您不能同时运行多个 LibreOffice 实例。
另外,请在此处查看相同的解决方案。
在 IIS 上通过 PHP 执行程序时,Windows 的用户帐户是 IUSR 帐户。
所以,
C:\\inetpub\\wwwroot\\path
目录。soffice.com
LibreOffice 路径(如果在执行时提供完整路径,则不需要):chdir("C:\\Program Files\\LibreOffice\\program");
wwwroot
(这一步可以是可选的):putenv("HOME=C:\\inetpub\\wwwroot\\temp");
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;
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删除目录与它的文件如图所示这里。
作为服务运行的 LibreOffice 和请求文档转换的 PHP 可能是更好的解决方案,但这篇文章涉及每次需要文档转换时调用 LibreOffice,然后让它关闭。 也可以使用单独的处理方法来避免等待 LibreOffice 完成转换。
www-data
类似步骤,因为它是 Linux 中 apache 的用户帐户。 请参阅此处的解决方案。env
路径将类似于:\"-env:UserInstallation=file:///var/www/tmp\"
注意三个/
after file:
不管是 Windows 还是 Linux,即使 Linux 中的路径以/
开头,或者它们可能忽略 Windows 中的斜杠,但对我来说似乎不一致。
INSHAALLAH 它会在那之后工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.