无法通过 php 中的 shell_exec() 函数 运行 wkhtmltopdf 命令,但相同的命令在命令行上有效
Not able to run wkhtmltopdf commad through shell_exec() function in php but same command works on command line
我有麻烦了,对 php shell_exec 命令非常困惑。
当命令由 PHP 执行时,我没有错误,但执行失败。如果我从终端使用完全相同的命令,它就可以工作。
命令如下:
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
当我从终端启动时:
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
但是从我的 php 脚本来看:
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
我测试了shell_exec的输出,它是空的。
日志:
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
/tmp 目录中没有权限 pb :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
我已经尝试使用 PHP exec() 函数来获取错误信息,我只是在 return_var 中得到一个“1”错误代码,而在输出中什么也没有。
有关信息,此问题出现在我的测试服务器、我的台式电脑上,但没有出现在我的笔记本电脑上。所有 3 个都具有相同的 PHP、Apache、Mysql 版本。
我什么都不懂...
感谢您的帮助,我正在失去理智。
大卫.
看起来像是用户的权限问题。
当您从终端运行命令时,它是当前使用的用户帐户,它确实具有正确的权限运行 /usr/bin中的命令,并且执行特定文件。
当你从php脚本中运行它时,它是你系统上的http服务器帐户,需要权限才能执行/usr/bin中的文件。通常这是 apache 用户。
您应该如何设置权限取决于您的系统。请记住,apache 允许的内容也适用于访问您的 http 服务器的任何人。
是共享主机吗? shell_exec 似乎是一个受限函数。在调用 shell_exec.
之前尝试 运行 error_reporting(E_ALL); ini_set('display_errors', 1);
我在这里找到了解决方案:Executing wkhtmltopdf from PHP fails
感谢 Krzychu。
首先从shell_exec
命令获取信息在命令末尾添加“2>&1”。通过这种方式,您将在命令的 return 中获得信息:
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
解决方法:
不使用 wkhtmltopdf ubuntu 包 (0.9.9-4)
的官方包
所以不需要安装xvfb!(这个建议我已经看过很多次了)
我遇到这个问题已经很久了,添加 . ' 2>&1'
在 $command
以某种方式解决了问题之后。
这个:
$output = shell_exec($command . ' 2>&1');
而不是:
$output = shell_exec($command);
不知道为什么,但它有效,我很感激。
我偶然发现了同样的问题,在我的例子中,像 /var/www 这样的 exec 命令中的绝对路径不起作用,我不得不从执行 php 的地方开始使用相对路径文件。
我还想注意到,使用 shell_exec 它不起作用,但是它使用普通的 exec 命令起作用,不确定这里的区别在哪里。
我有麻烦了,对 php shell_exec 命令非常困惑。 当命令由 PHP 执行时,我没有错误,但执行失败。如果我从终端使用完全相同的命令,它就可以工作。
命令如下:
/usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyxa9otq.html" "/tmp/knplabs_snappyv3pD7h.pdf"
当我从终端启动时:
$ /usr/bin/wkhtmltopdf --lowquality --dpi 300 --encoding utf-8 "/tmp/knplabs_snappyWG9XTd.html" "/tmp/knplabs_snappyv3pD7h.pdf"
Loading page (1/2)
Printing pages (2/2)
Done
但是从我的 php 脚本来看:
// Construct the previous command
$command = $this->buildCommand($url, $path);
../..
shell_exec($command);
../..
$content = file_get_contents($path);
../..
我测试了shell_exec的输出,它是空的。
日志:
Warning: file_get_contents(/tmp/knplabs_snappyv3pD7h.pdf): failed to open stream: No such file or directory in /*****/lib/snappy/SnappyMedia.class.php on line 64
/tmp 目录中没有权限 pb :
$ ls -la /tmp
total 448
drwxrwxrwt 16 root root 4096 mars 12 21:51 .
../..
我已经尝试使用 PHP exec() 函数来获取错误信息,我只是在 return_var 中得到一个“1”错误代码,而在输出中什么也没有。
有关信息,此问题出现在我的测试服务器、我的台式电脑上,但没有出现在我的笔记本电脑上。所有 3 个都具有相同的 PHP、Apache、Mysql 版本。 我什么都不懂...
感谢您的帮助,我正在失去理智。 大卫.
看起来像是用户的权限问题。
当您从终端运行命令时,它是当前使用的用户帐户,它确实具有正确的权限运行 /usr/bin中的命令,并且执行特定文件。
当你从php脚本中运行它时,它是你系统上的http服务器帐户,需要权限才能执行/usr/bin中的文件。通常这是 apache 用户。
您应该如何设置权限取决于您的系统。请记住,apache 允许的内容也适用于访问您的 http 服务器的任何人。
是共享主机吗? shell_exec 似乎是一个受限函数。在调用 shell_exec.
之前尝试 运行error_reporting(E_ALL); ini_set('display_errors', 1);
我在这里找到了解决方案:Executing wkhtmltopdf from PHP fails
感谢 Krzychu。
首先从shell_exec
命令获取信息在命令末尾添加“2>&1”。通过这种方式,您将在命令的 return 中获得信息:
$no_output = shell_exec($command);
echo $no_output; // nothing
$output = shell_exec($command . ' 2>&1');
echo $output; // in my case : "cannot connect to X server"
解决方法:
不使用 wkhtmltopdf ubuntu 包 (0.9.9-4)
- 的官方包
所以不需要安装xvfb!(这个建议我已经看过很多次了)
我遇到这个问题已经很久了,添加 . ' 2>&1'
在 $command
以某种方式解决了问题之后。
这个:
$output = shell_exec($command . ' 2>&1');
而不是:
$output = shell_exec($command);
不知道为什么,但它有效,我很感激。
我偶然发现了同样的问题,在我的例子中,像 /var/www 这样的 exec 命令中的绝对路径不起作用,我不得不从执行 php 的地方开始使用相对路径文件。
我还想注意到,使用 shell_exec 它不起作用,但是它使用普通的 exec 命令起作用,不确定这里的区别在哪里。