PHP的exec()函数无返回值排查方法

在安全imagemagic时 需要用到 exec很多服务器上安装失败

exec()执行外部命令失败,但没有任何错误信息。 


exec执行某命令在命令行下没有问题,但是在php中就出错。这个问题99.99%与权限有关,但是exec执行的命令不会返回错误。一个技巧就是使用管道命令,假设你的exec调用如下:

exec('convert a.jpg b.jpg', $output, $return_val);

可以更改如下:

exec('convert a.jpg b.jpg 2>&1', $output, $return_val);  
print_r($output);

使用 2>&1, 命令就会输出shell执行时的错误到$output变量, 输出该变量即可分析。

备注: exec有3个参数,第一个是要执行的命令,第二个是参数是一个数组,数组的值是由第一个命令执行后生成的,第三个参数执行的状态,0表示成功,其他都表示失败

在php里面一共有三个函数可以用来执行外部命令system,exec,passthru

更多精彩博文请到言会咸的博客

网址:http://blog.csdn.net/aoyoo111


发布了37 篇原创文章 · 获赞 4 · 访问量 58万+
展开阅读全文

PHP exec输出不返回并执行命令

10-04

<div class="post-text" itemprop="text"> <p>I am trying to execute a command in PHP on a Windows Server Machine.</p> <p>The code i am using is:</p> <pre><code><?php $out = array(); exec('C:\libs\bin\youtube-dl -o "C:\xampp\htdocs\API\jbb15\convert\SrMKY5Aoyrw" "http://youtube.com/watch?v=SrMKY5Aoyrw"', $out); print_r($out); ?> </code></pre> <p>My result in PHP is:</p> <pre><code>Array ( [0] => [youtube] SrMKY5Aoyrw: Downloading webpage [1] => [youtube] SrMKY5Aoyrw: Downloading video info webpage [2] => [youtube] SrMKY5Aoyrw: Extracting video information [3] => [youtube] SrMKY5Aoyrw: Downloading DASH manifest [4] => [youtube] SrMKY5Aoyrw: Downloading DASH manifest ) </code></pre> <p>My result should be:</p> <pre><code>C:\xampp\htdocs\API\jbb15\convert>C:\libs\bin\youtube-dl -o "C:\xampp\htdocs\API\jbb15\convert\SrMKY5Aoyrw" "http://youtube.com/watch?v=SrMKY5Aoyrw" [youtube] SrMKY5Aoyrw: Downloading webpage [youtube] SrMKY5Aoyrw: Downloading video info webpage [youtube] SrMKY5Aoyrw: Extracting video information [youtube] SrMKY5Aoyrw: Downloading DASH manifest [youtube] SrMKY5Aoyrw: Downloading DASH manifest WARNING: Requested formats are incompatible for merge and will be merged into mkv. [download] Destination: C:\xampp\htdocs\API\jbb15\convert\SrMKY5Aoyrw.f137 [download] 100% of 111.72MiB in 00:01 [download] Destination: C:\xampp\htdocs\API\jbb15\convert\SrMKY5Aoyrw.f251 [download] 100% of 8.13MiB in 00:00 [ffmpeg] Merging formats into "C:\xampp\htdocs\API\jbb15\convert\SrMKY5Aoyrw.mkv" Deleting original file C:\xampp\htdocs\API\jbb15\convert\SrMKY5Aoyrw.f137 (pass -k to keep) Deleting original file C:\xampp\htdocs\API\jbb15\convert\SrMKY5Aoyrw.f251 (pass -k to keep) </code></pre> <p>The problem about this is: The exact same command as in the above PHP script works perfectly if the command is copied into the Windows CMD. There are no PHP error logs. The command --verbose will return nothing in PHP, but works in windows cmd.</p> </div> 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览