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

前端之家收集整理的这篇文章主要介绍了php – 脚本在命令行后不会继续前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带命令行的脚本的问题.. PHP脚本永远不会继续..

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

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

PDF格式

http://docdro.id/b0M5vfw

  1. $Cmd = new Command;
  2. if($err = $Cmd->exec('/var/bin/poppler-0.51.0/utils/pdfimages -list /var/test.pdf')){
  3. echo "ERR: $err\n";
  4. }
  5. echo "continue\n";

  1. class Command {
  2. private $descriptorspec;
  3.  
  4. private $output = '';
  5.  
  6. private $process;
  7. private $pipes = [];
  8.  
  9. public function __construct(){
  10. $this->descriptorspec = [
  11. 0 => ['pipe','r'],// stdin
  12. 1 => ['pipe','w'],// stdout
  13. 2 => ['pipe','w'] // stderr
  14. ];
  15. }
  16.  
  17. public function output(): string{
  18. return $this->output;
  19. }
  20.  
  21. public function close(){
  22. foreach($this->pipes as $pipe){
  23. if(is_resource($pipe)){
  24. fclose($pipe);
  25. }
  26. }
  27.  
  28. proc_close($this->process);
  29. }
  30.  
  31. public function exec(string $Syntax){
  32. $this->process = proc_open($Syntax,$this->descriptorspec,$this->pipes);
  33. fclose($this->pipes[0]);
  34.  
  35. $this->output = stream_get_contents($this->pipes[1]);
  36.  
  37. $stderr = stream_get_contents($this->pipes[2]);
  38.  
  39. $this->close();
  40.  
  41. return $stderr;
  42. }
  43. }

错误

  1. # /var/bin/poppler-0.51.0/utils/pdfimages -list /var/test.pdf
  2. page num type width height color comp bpc enc interp object ID x-ppi y-ppi size ratio
  3. --------------------------------------------------------------------------------------------
  4. 1 0 image 2154 303 rgb 3 8 jpeg yes [inline] 289 292 - -
  5. Syntax Error (50560): Illegal character '>'
  6. Syntax Error (50560): Unknown operator '<10><07><82>;w<ad><a2><b4>2r<1f><10><07><8f>~j<c4>Hq<cf>Z<86>'
  7. Syntax Error (50568): Unknown operator '<0f><b5>X<8f><ae><d0>:<d7>DU<91><cb>'v'
  8. Syntax Error (50568): Illegal character ')'
  9.  
  10. ........
  11.  
  12. Syntax Error (66698): Illegal character <04> in hex string
  13. Syntax Error (66699): Illegal character <ff> in hex string
  14. Syntax Error (66699): Illegal character <c1> in hex string
  15. Syntax Error (66705): Unknown operator '<9b>'
  16. Syntax Error (66714): Illegal character ')'
  17. Syntax Error (66714): Unknown operator '<bc>q<ff>'
  18. Syntax Error (66720): Unknown operator '<05>6<f8><c2><fa><d7><c3>?<f8>'
  19. Syntax Error (66741): Unknown operator '<df><ec><99><e1>-'
  20. Syntax Error (66743): Unknown operator ']'
  21. Syntax Error (66762): Unknown operator '<cc>'
  22. Syntax Error: Unterminated string
  23. Syntax Error: End of file inside array
  24. Syntax Error: End of file inside array
  25. 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语句,你应该保留对脚本的控制.

  1. $Cmd = new Command;
  2.  
  3. try {
  4. $err = $Cmd->exec('/var/bin/poppler-0.51.0/utils/pdfimages - list/var/test.pdf')){
  5. } catch (Exception $e) {
  6. var_log($e);
  7. }
  8.  
  9. echo "continue\n";

猜你在找的PHP相关文章