共計 618 個字符,預計需要花費 2 分鐘才能閱讀完成。
要在 PHP 中執行 shell 命令,可以使用 exec() 函數、shell_exec() 函數或 system() 函數。
- exec() 函數:
exec(command, output, return_var);
- command:要執行的 shell 命令。
- output:可選參數,用于存儲命令的輸出結果。
- return_var:可選參數,用于存儲命令的返回值。
示例:
exec('ls -l', $output, $return_var);
print_r($output); // 輸出命令執行結果
echo $return_var; // 輸出命令返回值
- shell_exec() 函數:
$output = shell_exec(command);
- command:要執行的 shell 命令。
示例:
$output = shell_exec('ls -l');
echo $output; // 輸出命令執行結果
- system() 函數:
system(command, return_var);
- command:要執行的 shell 命令。
- return_var:可選參數,用于存儲命令的返回值。
示例:
system('ls -l', $return_var);
echo $return_var; // 輸出命令返回值
注意:
- 使用 exec()、shell_exec() 或 system() 函數執行 shell 命令時,請確保你信任該命令,以防止安全風險。
- 在某些環境下,可能需要在 PHP 配置文件中啟用相關函數。
丸趣 TV 網 – 提供最優質的資源集合!
正文完