在php代碼中執(zhí)行cmd命令的方法,介紹下在php.ini文件中配置safe_mode參數(shù)支持命令執(zhí)行的方法,有需要的朋友參考下。
說(shuō)明:
本節(jié)內(nèi)容在wamp包安裝的環(huán)境實(shí)現(xiàn)。
首先,打開(kāi)php.ini,關(guān)掉安全模式safe_mode = off,然后在看看 禁用函數(shù)列表 disable_functions = proc_open, popen, exec, system, shell_exec ,把exec去掉。
php代碼:
<?php
exec("mkdir d:\test",$out);
print_r($out);
?>
執(zhí)行該php文件,會(huì)發(fā)現(xiàn)在d盤(pán)下多了一個(gè)test文件夾。
參考文檔:
exec函數(shù)解析
exec語(yǔ)法: string exec(string command, string [array], int [return_var]);
exec返回值: 字符串
exec參數(shù)說(shuō)明
Command – 需要執(zhí)行的命令
Array – 是輸出值
return_var –是返回值0或1,如果返回0則執(zhí)行成功,返回1則執(zhí)行失敗。
exec不成功,調(diào)試方案
使用管道命令, 使用 2>&1, 命令就會(huì)輸出shell執(zhí)行時(shí)的錯(cuò)誤到$output變量, 輸出該變量即可分析。
例如:
exec(‘convert a.jpg b.jpg', $output, $return_val);
修改為:
exec(‘convert a.jpg b.jpg 2>&1′, $output, $return_val);
print_r($output);
提示需要檢查php.ini中時(shí)候禁用了這個(gè)函數(shù),其次是安全模式時(shí)候開(kāi)啟。