在PHP中,exec()
函數允許你執行外部命令
exec('/usr/bin/python3 /path/to/your/script.py');
exec()
函數,而不是將它們連接在一起。這將使參數更容易被外部命令解析。exec('/usr/bin/python3 /path/to/your/script.py arg1 arg2 arg3');
exec()
函數。exec('/usr/bin/python3 /path/to/your/script.py', $args);
2>&1
將錯誤輸出重定向到標準輸出,這樣你可以捕獲并處理錯誤消息。exec('/usr/bin/python3 /path/to/your/script.py 2>&1', $output, $return_var);
if ($return_var !== 0) {
echo "Error: " . implode("\n", $output);
} else {
echo "Success: " . implode("\n", $output);
}
shell_exec()
:如果你需要獲取命令的完整輸出,可以使用shell_exec()
函數。$output = shell_exec('/usr/bin/python3 /path/to/your/script.py 2>&1');
echo "Success: " . $output;
passthru()
:如果你想要直接將命令的輸出傳遞給瀏覽器,而不經過PHP處理,可以使用passthru()
函數。passthru('/usr/bin/python3 /path/to/your/script.py 2>&1');
通過遵循這些建議,你可以確保在使用exec()
函數處理外部命令時實現標準化。