你好我有一个 php 文件有 2 个按钮来执行一个 sh 文件并且在 sh 文件中想要删除 txt 文件我可以从终端使用 sh 文件但我不能在网络浏览器中这样做
php文件
<?php
if($_GET){
if(isset($_GET['test'])){
test();
}elseif(isset($_GET['delete'])){
delete();
}
}
function test()
{
$old_path = getcwd();
chdir('/var/www/html/admin');
$output= shell_exec("./test.sh");
chdir($old_path);
echo "<h1>$output</h1>";
}
function delete()
{
$old_path = getcwd();
chdir('/var/www/html/admin');
$output= shell_exec("./del.sh");
chdir($old_path);
echo "<h1>$output</h1>";
}
?>
<input type="submit" name="delete" class="btn btn-danger" value="delete">
<input type="submit" name= "test" class="btn btn-primary" value="test">
当我按下它打印在我屏幕上但没有运行进程的按钮时,有 sh 文件要执行或删除文件
#!/bin/bash
echo 'delete file';
cd /var/www/html/test
rm text.txt
最佳答案
尝试将您的输入放在一个表单中:
<form action="/" method="get">
<input type="submit" name="delete" class="btn btn-danger" value="delete">
<input type="submit" name= "test" class="btn btn-primary" value="test">
</form>
你的 php 文件:
<?php
if($_GET){
if(isset($_GET['test'])){
test();
}elseif(isset($_GET['delete'])){
delete();
}
}
function test()
{
$old_path = getcwd();
chdir('/var/www/html/admin');
$output= shell_exec("./test.sh");
chdir($old_path);
echo "<h1>$output</h1>";
}
function delete()
{
$old_path = getcwd();
chdir('/var/www/html/admin');
$output= shell_exec("./del.sh");
chdir($old_path);
echo "<h1>$output</h1>";
}
?>
<form action="/" method="get">
<input type="submit" name="delete" class="btn btn-danger" value="delete">
<input type="submit" name= "test" class="btn btn-primary" value="test">
</form>
关于php - 使用php在web中使用sh文件删除文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53772945/