简体   繁体   English

无法使用PHP执行外部进程

[英]Can't execute external process with PHP

I have the following code 我有以下代码

function generate_pdf() {

        $fdf_data_strings = $this->get_hash_for_pdf();
        #$fdf_data_names   = array('49a' => "yes");
        $fdf_data_names = array();
        $fields_hidden    = array();
        $fields_readonly  = array();
        $hud_pdf = ABSPATH.'../pdf/HUD3.pdf';

        $fdf= forge_fdf( '',
                 $fdf_data_strings,
                 $fdf_data_names,
                 $fields_hidden,
                 $fields_readonly );

        /*  echo "<pre>";
            print_r($fdf);
            echo "</pre>";
            die('');
        */

        $fdf_fn= tempnam( '.', 'fdf' );
        $fp= fopen( $fdf_fn, 'w' );
        if( $fp ) {
          fwrite( $fp, $fdf );
            //$data=fread( $fp, $fdf );
         // echo $data;
          fclose( $fp );


          header( 'Content-type: application/pdf' );
          header( 'Content-disposition: attachment; filename=settlement.pdf' ); // prompt to save to disk

          passthru( 'pdftk HUD3.pdf fill_form '. $fdf_fn.' output - flatten');

          unlink( $fdf_fn ); // delete temp file

        }
        else { // error
          echo 'Error: unable to open temp file for writing fdf data: '. $fdf_fn;
        }
    }
}

is there anything wrong with it? 它有什么问题吗?

the problem is, I have installed pdftk 问题是,我已经安装了pdftk

runing whereis pdftk gives me '/usr/local/bin/pdftk' 跑到whereis pdftk给我'/usr/local/bin/pdftk'

physically checked the location, pdftk is there at the said location.. 实际检查了位置,pdftk在那个位置..

using terminal, if i run pdftk --version or any other command, it runs 使用终端,如果我运行pdftk --version或任何其他命令,它运行

if I use php like passthru('/usr/local/bin/pdftk --version') nothing is displayed 如果我使用像passthru('/usr/local/bin/pdftk --version')这样的PHP, passthru('/usr/local/bin/pdftk --version')显示任何内容

if I used php like system("PATH=/usr/local/bin && pdftk --version"); 如果我使用php喜欢system("PATH=/usr/local/bin && pdftk --version"); it says '/usr/local/bin /pdftk :there is no directory of file ' 它说'/usr/local/bin /pdftk :there is no directory of file '

when I run this function script , prompt for file download pops, but when i save it, nothng is saved, 当我运行这个函数脚本时,提示文件下载弹出,但是当我保存它时,nothng被保存,

i have checked permission for this folder and changed it 0755, 0766, 0777, 0666 i have tried all, nothng works 我已经检查权限,该文件夹并改变了它0755, 0766, 0777, 0666我已经尝试了所有,nothng作品

For 3 days, i am striving to get over it, and I have asked question regarding this too, but Can't figure out what the hell is going on with me. 3天,我正在努力克服它,我也问过这个问题,但无法弄清楚到底是怎么回事。

Can somebody help me before i strike my head with wall? 在我撞墙之前,有人可以帮助我吗?

The pasthru function does not execute the program through the shell. pasthru函数不通过shell执行程序。

Pass the exact path into the passthru command. 将准确的路径传递给passthru命令。

Eg 例如

passthru( '/usr/local/bin/pdftk HUD3.pdf fill_form '. $fdf_fn.' output - flatten');

or passthru( '/usr/local/bin/pdftk' . $hud_pdf . 'fill_form '. $fdf_fn.' output - flatten'); 或者passthru('/ usr / local / bin / pdftk'。$ hud_pdf。'fill_form'。$ fdf_fn。'output - flatten');

If this still doesn't work test using <?php passthru("/path/to/pdftk --help"); ?> 如果仍然无法使用<?php passthru("/path/to/pdftk --help"); ?> <?php passthru("/path/to/pdftk --help"); ?> where /path/to/pdftk is your path returned by which or where is, to ensure path is correct. <?php passthru("/path/to/pdftk --help"); ?>其中/ path / to / pdftk是您返回的路径,由哪个或哪个位置确保路径正确。

If path is correct then the issue may be related to permissions either on the temporary directory you tell pdftk to use or the permissions on the pdftk binary with regards to the apache user. 如果路径正确,那么问题可能与您告诉pdftk使用的临时目录上的权限或者关于apache用户的pdftk二进制文件的权限有关。

If these permissions are fine you can verify the pdftk starts up from php but hangs from running your command, then might be able to try the workaround listed here . 如果这些权限很好,你可以验证pdftk是从php启动但是从运行命令中挂起,然后可能可以尝试这里列出的解决方法。

Further documentation on passthru is avaliable passthru PHP Manual . 关于passthru的进一步文档是可用的passthru PHP手册

As a side note, the putenv php function is used to set environment variables. 作为旁注, putenv php函数用于设置环境变量。

Eg putenv('PATH='.getenv('PATH').':.'); 例如putenv('PATH='.getenv('PATH').':.');

All 3 PHP functions: exec(), system() and passthru() executes an external command, but the differences are: 所有3个PHP函数:exec(),system()和passthru()执行外部命令,但不同之处是:

  • exec(): returns the last line of output from the command and flushes nothing. exec():返回命令的最后一行输出并且不刷新任何内容。
  • shell_exec(): returns the entire output from the command and flushes nothing. shell_exec():返回命令的整个输出并且不刷新任何内容。
  • system(): returns the last line of output from the command and tries to flush the output buffer after each line of the output as it goes. system():返回命令的最后一行输出,并尝试在输出的每一行之后刷新输出缓冲区。
  • passthru(): returns nothing and passes the resulting output without interference to the browser, especially useful when the output is in binary format. passthru():不返回任何内容并传递结果输出​​而不会干扰浏览器,尤其在输出为二进制格式时非常有用。

Also see PHP exec vs-system vs passthru SO Question . 另请参阅PHP exec vs-system vs passthru SO Question

The implementation of these functions is located at exec.c and uses popen. 这些函数的实现位于exec.c并使用popen。

I had the same issue and this is working after lots of experiments : 我有同样的问题,经过大量实验后,这是有效的:

function InvokePDFtk($pdf_tpl, $xfdf,$output){

        $params=" $pdf_tpl fill_form $xfdf output $output flatten 2>&1";

        $pdftk_path=exec('/usr/bin/which /usr/local/bin/pdftk');

        $have_pdftk= $pdftk_path=='/usr/local/bin/pdftk' ;

        $pdftk_path=$have_pdftk  ? $pdftk_path  : 'pdftk ';

        exec($pdftk_path.$params,$return_var);

        return  array('status'=> $have_pdftk,
                     'command' =>$pdftk_path.$params, 'output'=>$return_var);
    }

hope this might give you some insight . 希望这可能会给你一些见解。 (change according to your needs) (根据您的需要改变)

Completing Appleman answer, those 3 functions can be considered as dangerous, because they allow you execute program using php, thus an attacker that exploited one of your script if you are not careful enougth. 完成Appleman的回答,这三个函数可以被认为是危险的,因为它们允许您使用php执行程序,因此如果您不小心使用,攻击者会利用您的一个脚本。 So in many php configuration that want to be safe they are disabled. 因此,在许多想要安全的php配置中,它们被禁用。

So you should check for the disable_functions directive in you php.ini(and any php configuration file) and see if the function you use is disabled. 所以你应该检查你的php.ini(和任何php配置文件)中的disable_functions指令,看看你使用的功能是否被禁用。

Perhaps you should keep fclose out of the if statement, make sure you have it directed to the right file! 也许你应该将fclose保留在if语句之外,确保你将它指向正确的文件! :) :)

Is your web server chrooted? 你的网络服务器是否被chrooted? Try putting the executable into a directory that is viewable by the server. 尝试将可执行文件放入服务器可查看的目录中。

使用安全模式四处游走并明确检查您的Web服务器日志文件,通常在:

/var/log/apache2/error.log

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

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