PDFTk适用于本地但不在私有服务器上工作

PDFTk适用于本地但不在私有服务器上工作

问题描述:

I have private server CentOS 6 and I have installed pdftk program to generate pdf files. When I connect with SSH client, I can run pdftk program successfully. But I couldn't in php with exec() function.

I have a very simple php file as shown below. This is only to test if pdftk is working or not. When I ran this file on my localhost with xampp, it generates the file but when I tried on my private server, not gives error and not generates the file. I am not expert and expecting any help from you. Thanks in advance.

PHP CODE:

<?php
exec("pdftk form.pdf output private.pdf");

The error look like this:

Array ( [0] => Error: Failed to open output file:
  [1] => collated.pdf [2] => No output created.) 

Note: I have tried this code on putty ssh client and works perfectly.

我有私人服务器CentOS 6,我安装了 pdftk code>程序来生成pdf文件。 当我连接SSH客户端时,我可以成功运行pdftk程序。 但我无法在 php code>中使用 exec() code>函数。 p>

我有一个非常简单的php文件,如下所示。 这只是为了测试pdftk是否正常工作。 当我使用xampp在我的localhost上运行此文件时,它会生成该文件,但是当我尝试使用我的私有服务器时,不会出错并且不会生成文件。 我不是专家,期待你的任何帮助。 在此先感谢。 p>

PHP代码: p>

 &lt;?php 
exec(“pdftk form.pdf output private.pdf”)  ; 
  code>  pre> 
 
 

错误如下所示: p>

  Array([0] =&gt;错误:失败 打开输出文件:
 [1] =&gt; collat​​ed.pdf [2] =&gt;没有创建输出。)
  code>  pre> 
 
 

注意:我试过这段代码 在putty ssh客户端上并且工作得很好。 p> div>

The error is: Array ( [0] => Error: Failed to open output file: [1] => collated.pdf [2] => No output created. . same exec code on putty works fine.

The difference you can spot is in the user running the code. In case of PuTTY, you are logged in as a different user than the user who is running your script when accessed from web. Since you are creating a new file, the user needs a write access to the directory, where you are. This is generally a bad idea to allow to write that user to the directory, where your scripts are so it is a good idea to create a new directory (such as export), where the apache user will have access to write:

mkdir export
chown apache:apache export
chmod 755 export

and modify your script to write a file into that directory:

exec("pdftk form.pdf output export/private.pdf");