duang5049 2013-01-11 08:25
浏览 77
已采纳

PHP中的Exec不起作用,而从提示符运行时该命令工作正常

I am trying to perform an OCR using tesseract on a Windows based Apache server. It makes me mad. The following doesn't produce the expected E:\zzz.txt file:

<?php

$command = "\"C:\\ocr\\tesseract.exe\" \"E:\\www\\test\\upload\\proofs\\src\\yehia.raef.jpg\" \"E:\\zzz\" 2>&1";

print $command;
exec("$command", $msg);
print_r($msg);

?>

When I run the program, here is what I get:

C:\Users\blob\Desktop>php ici.php
"C:\ocr\tesseract.exe" "E:\www\test\upload\proofs\src\yehia.raef.jpg" "E:\zzz" 2>&1

Array
(
    [0] => The filename, directory name, or volume label syntax is incorrect.
)

But now if run the command directly from the prompt:

C:\Users\blob\Desktop>"C:\ocr\tesseract.exe" "E:\www\test\upload\proofs\src\yehia.raef.jpg" "E:\zzz" 2>&
Tesseract Open Source OCR Engine v3.02 with Leptonica

It works like a charm. Please note this kind of command perfectly works with other software such as ImageMagick...

Help!

  • 写回答

1条回答 默认 最新

  • douwei8295 2013-01-11 08:35
    关注

    Asuming that PHP has all the required permissions, in PHP versions earlier than PHP/5.3.0 running on Windows you need to add an extra set of quotes around your full command:

    <?php
    
    $command = "\"C:\\ocr\\tesseract.exe\" \"E:\\www\\test\\upload\\proofs\\src\\yehia.raef.jpg\" \"E:\\zzz\" 2>&1";
    if( PHP_OS=='WINNT' && version_compare(PHP_VERSION, '5.3.0', '<') ){
        $command = '"' . $command . '"';
    }
    print $command;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实验stm32主通道和互补通道独立输出
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题