加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 脚本在命令行后不会继续

发布时间:2020-12-13 13:26:40 所属栏目:PHP教程 来源:网络整理
导读:我有一个带命令行的脚本的问题.. PHP脚本永远不会继续.. 试图直接通过putty调用命令行,它输出很多错误但是立即返回/完成.为什么不返回PHP呢? 它适用于其他PDF文件,但不适用于此 PDF格式 http://docdro.id/b0M5vfw 码 $Cmd = new Command;if($err = $Cmd-exe
我有一个带命令行的脚本的问题.. PHP脚本永远不会继续..

试图直接通过putty调用命令行,它输出很多错误但是立即返回/完成.为什么不返回PHP呢?

它适用于其他PDF文件,但不适用于此

PDF格式

http://docdro.id/b0M5vfw

$Cmd = new Command;
if($err = $Cmd->exec('/var/bin/poppler-0.51.0/utils/pdfimages -list /var/test.pdf')){
    echo "ERR: $errn";
}
echo "continuen";

class Command {
    private $descriptorspec;

    private $output = '';

    private $process;
    private $pipes = [];

    public function __construct(){
        $this->descriptorspec = [
            0 => ['pipe','r'],// stdin
            1 => ['pipe','w'],// stdout
            2 => ['pipe','w']  // stderr
        ];
    }

    public function output(): string{
        return $this->output;
    }

    public function close(){
        foreach($this->pipes as $pipe){
            if(is_resource($pipe)){
                fclose($pipe);
            }
        }

        proc_close($this->process);
    }

    public function exec(string $syntax){
        $this->process = proc_open($syntax,$this->descriptorspec,$this->pipes);
        fclose($this->pipes[0]);

        $this->output = stream_get_contents($this->pipes[1]);

        $stderr = stream_get_contents($this->pipes[2]);

        $this->close();

        return $stderr;
    }
}

错误

# /var/bin/poppler-0.51.0/utils/pdfimages -list /var/test.pdf
page   num  type   width height color comp bpc  enc interp  object ID x-ppi y-ppi size ratio
--------------------------------------------------------------------------------------------
   1     0 image    2154   303  rgb     3   8  jpeg   yes  [inline]     289   292    -    -
Syntax Error (50560): Illegal character '>'
Syntax Error (50560): Unknown operator '<10><07><82>;w<ad><a2><b4>2r<1f><10><07><8f>~j<c4>Hq<cf>Z<86>'
Syntax Error (50568): Unknown operator '<0f><b5>X<8f><ae><d0>:<d7>DU<91><cb>'v'
Syntax Error (50568): Illegal character ')'

........

Syntax Error (66698): Illegal character <04> in hex string
Syntax Error (66699): Illegal character <ff> in hex string
Syntax Error (66699): Illegal character <c1> in hex string
Syntax Error (66705): Unknown operator '<9b>'
Syntax Error (66714): Illegal character ')'
Syntax Error (66714): Unknown operator '<bc>q<ff>'
Syntax Error (66720): Unknown operator '<05>6<f8><c2><fa><d7><c3>?<f8>'
Syntax Error (66741): Unknown operator '<df><ec><99><e1>-'
Syntax Error (66743): Unknown operator ']'
Syntax Error (66762): Unknown operator '<cc>'
Syntax Error: Unterminated string
Syntax Error: End of file inside array
Syntax Error: End of file inside array
Syntax Error: Leftover args in content stream
PDF存在问题 – @dwarring已经在评论中提到了这一点(此处引用以表达对评论者的评价)

@dwarring said “just quickly,I’m pretty sure that this PDF is dying because the content stream contains an inline image started by and ‘BI’ followed by random data and ended by ‘EI’. The Adobe engineers were having an off-day when they designed these operators,the problem being that situations arise where the binary data randomly contains ‘EI’ and makes the PDF unparsable. Some tools may handle this better,but ideally the producer of this image should avoid the use of inline images.”

但是,从PHP方面来看,使用try / catch块而不是if语句,你应该保留对脚本的控制.

$Cmd = new Command;

try {
    $err = $Cmd->exec('/var/bin/poppler-0.51.0/utils/pdfimages - list/var/test.pdf')){
} catch (Exception $e) {
    var_log($e);
}

echo "continuen";

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读