我正在嘗試使用 PHP 從 HTML 頁面執行 shell 腳本。我在這里找到了一個我一直試圖遵循的先前示例,但我遇到了問題。我不確定原因是什么,但沒有回傳錯誤,也沒有從 bash 腳本創建檔案。
索引.php:
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form action="./test.php">
<input type="submit" value="Open Script">
</form>
</body>
</html>
測驗檔案
<?php
exec("./bash_script.sh");
header('Location: http://local.server.edu/ABC/abc_test/');
?>
bash_script.sh
#!/bin/bash
touch ./test_file.txt
我注意到可能是問題原因的一件事是本地服務器上的路徑似乎與檔案系統不完全匹配。
如果我將腳本中的所有相對路徑切換為絕對路徑,例如:
/local/sequence/temp/abc_test/file.exe
然后單擊按鈕運行腳本后,我收到一條錯誤訊息:
The requested URL /local/sequence/temp/abc_test/test.php was not found on this server
編輯:他們位于的三個檔案/local/sequence/temp/abc_test并且有一個符號鏈接指向該目錄在/export/www/htdocs/ABC
uj5u.com熱心網友回復:
錯誤訊息似乎表明未找到 test.php。正如所寫,它需要與 index.php 位于同一目錄中
您已經測驗了實際的 bash 腳本,因此我們可以繼續假設它正在執行接收提交的腳本。
我建議將所有網路內容放在一個頁面中,因為您可以測驗發送和接收輸入。
<?php
// for testing
// exec("./bash_script.sh");
// check for POST submission (this is not just reading data)
if(isset($_POST['runScript'])) {
// die('Request received');
exec("./bash_script.sh");
// It’s always proper to redirect after post :)
header('Location: http://local.server.edu/ABC/abc_test/');
die;
}
// finished with logic; show form
?>
<!DOCTYPE html>
<html>
<head>
<style></style>
</head>
<body>
<form method="POST">
<input type="submit" name="runScript" value="Open Script">
</form>
</body>
</html>
請注意,我將 name 屬性添加到提交按鈕,并在提交到呼叫頁面時使表單使用 POST 方法(沒有操作意味著提交給自己)。
我還留下了一些評論操作,以在必要時幫助除錯
您可能需要調整 bash 腳本的路徑。目前它會在與 index.php 相同的目錄中查找,這不是您想要在生產中做的事情。
uj5u.com熱心網友回復:
您將能夠以某種方式做到這一點,但允許從 php 頁面執行此類操作總是非常危險的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/333647.html
