PHP 四个执行系统命令函数:exec()、passthru()、system()、shell_exec()

php提供4种方法执行系统外部命令:exec()、passthru()、system()、 shell_exec()。
在开始介绍前,先检查下php配置文件php.ini中是有禁止这是个函数。找到 disable_functions,配置如下:

disable_functions =

如果“disable_functions=”后面有接上面四个函数,将其删除。
默认php.ini配置文件中是不禁止你调用执行外部命令的函数的。

方法一:exec()

function exec(string $command,array[optional] $output,int[optional] $return_value)

php代码:

<?php
    echo exec("ls",$file); 
    echo "</br>"; 
    print_r($file); 
?>

​

执行结果:

test.php
Array( [0] => index.php [1] => test.php)

知识点:
exec 执行系统外部命令时不会输出结果,而是返回结果的最后一行。

如果你想得到结果你可以使用第二个参数,让其输出到指定的数组,此数组一个记录代表输出的一行,即如果输出结果有20行,则这个数

猜你喜欢

转载自blog.csdn.net/JineD/article/details/120842055