简体   繁体   中英

deleting file using sh file in web with php

hi i have a php file there is 2 button to execute a sh file and in sh file want to remove txt file i can using sh file from terminal but i cant do it in web browser

php file

<?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">

And there is sh file to execute or deleting file when i press the button it printed on my screen but didnt running the process

#!/bin/bash
echo 'delete file';
cd /var/www/html/test
rm text.txt 

Try to put your inputs inside a form:

<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>

Your php file:

<?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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM