php 检查 exec() 是否成功运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11875820/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking exec() runs successfully or not
提问by soft genic
I have been trying to let know know if the exec()command in php executes successfully or not so i can echo certain messages accordingly.
I tried the following piece of code but the problem with it is that whether exec()runs successfully or not it always echo "PDF not created"and never echo pdf created successfully. Kindly let me know how can i perform the check on the execution of exec() so i can echo messages accordingly
Thanks,
我一直试图让知道exec()php 中的命令是否成功执行,以便我可以相应地回显某些消息。我尝试了下面的一段代码,但它的问题是无论是否exec()成功运行,它总是echo "PDF not created"并且永远不会成功创建 echo pdf。请让我知道如何对 exec() 的执行进行检查,以便我可以相应地回显消息 谢谢,
<?php
if (exec('C://abc//wkhtmltopdf home.html sample.pdf'))
echo "PDF Created Successfully";
else
echo "PDF not created";
?>
回答by Matt Williamson
According to PHP's exec quickref, you can pass pointers in to get the output and status of the command.
根据 PHP 的exec quickref,您可以传入指针以获取命令的输出和状态。
<?php
exec('C://abc//wkhtmltopdf home.html sample.pdf', $output, $return);
// Return will return non-zero upon an error
if (!$return) {
echo "PDF Created Successfully";
} else {
echo "PDF not created";
}
?>
If you want to enumerate the possible errors, you can find the codes over at hiteksoftware
如果你想列举可能的错误,你可以在hiteksoftware找到代码
回答by malhal
The correct way is to check that the $return_var was not set to zero because it is only set to zero when it is successful. In certain cases the exec can fail and the return_var is not set to anything. E.g. if the server ran out of disk space during the exec.
正确的方法是检查 $return_var 是否没有设置为零,因为它只有在成功时才设置为零。在某些情况下,exec 可能会失败并且 return_var 未设置为任何内容。例如,如果服务器在执行期间磁盘空间不足。
<?php
exec('C://abc//wkhtmltopdf home.html sample.pdf', $output, $return_var);
if($return_var !== 0){ // exec is successful only if the $return_var was set to 0. !== means equal and identical, that is it is an integer and it also is zero.
echo "PDF not created";
}
else{
echo "PDF Created Successfully";
}
?>
Note : do not initialize $return_var to zero
注意:不要将 $return_var 初始化为零
回答by deepcell
A simple sample:
一个简单的例子:
$ip = "192.168.0.2";
$exec = exec( "ping -c 3 -s 64 -t 64 ".$ip, $output, $return );
echo $exec;
echo "<br />----------------<br />";
print_r( $output );
echo "<br />----------------<br />";
print_r( $return );
In case of not ping or ERROR. ( ONE )
在不ping或ERROR的情况下。( 一 )
----------------
Array ( [0] => PING 192.168.0.2 (192.168.0.2) 64(92) bytes of data. [1] => [2] => --- 192.168.0.2 ping statistics --- [3] => 3 packets transmitted, 0 received, 100% packet loss, time 2016ms [4] => )
----------------
1
In case of success ( ZERO )
如果成功(零)
rtt min/avg/max/mdev = 4.727/18.262/35.896/13.050 ms
----------------
Array ( [0] => PING 192.168.0.2 (192.168.0.2) 64(92) bytes of data. [1] => 72 bytes from 192.168.0.2: icmp_req=1 ttl=63 time=14.1 ms [2] => 72 bytes from 192.168.0.2: icmp_req=2 ttl=63 time=35.8 ms [3] => 72 bytes from 192.168.0.2: icmp_req=3 ttl=63 time=4.72 ms [4] => [5] => --- 192.168.0.2 ping statistics --- [6] => 3 packets transmitted, 3 received, 0% packet loss, time 2003ms [7] => rtt min/avg/max/mdev = 4.727/18.262/35.896/13.050 ms )
----------------
0

