0

I'm a beginner with php and need to write a code that displays a list of folders and a delete-button near each of them, in order to be able to cancel them. Here's my code: ($array_dir is an array containing the names of the directories in the current folder)

$conto=count($array_dir);
echo"<table>";

for ($b=0;$b<$conto;$b++) {
    echo"<tr><td><a href=$array_dir[$b]>".$array_dir[$b].
    "</a><br>";
    echo"<form name='delete_dir_".$b."' action=
    '".$_SERVER['PHP_SELF']."' method='GET'>";
    echo"<input type='submit' name='butdelete".$b."' value='Delete'>";
    echo"</form></td><td>";
    $dir=$array_dir[$b];
    if ((isset($_GET['butdelete".$b."'])) && ($_GET['butdelete".$b."']==$dir)) {
        if(rmdir($dir)) {
            echo"The directory ".$dir." has been removed";
        }
        else  {
            echo"Could not remove directory ".$dir;
        }
    }
}

This output looks fine but when I click on the delete-button it doesn't delete the folder and doesn't even return any error. I can't really understand where the error is !

4

2 回答 2

1

Look at the colour coding, and you'll see you have a problem with your $_GET access.

In fact, the way your code is now, you are literally looking for a URL like:

http://example.com/mypage.php?butdelete%22.%24b.%22=delete-me

Try this instead:

$_GET['butdelete'.$b]

Side-note: Never use $_SERVER['PHP_SELF'] in an action. Instead, just use action="" to refer to the current page.

于 2013-01-07T17:54:48.643 回答
0

您的语法错误导致您出现问题。我已修复并将文件操作移至脚本顶部,以便您在执行删除时列出是最新的。

<?php 

//If form has been posted try and delete if dir exists in array
if((isset($_GET['butdelete'.$b])) && (array_search($_GET['butdelete'.$b], $array_dir)))
{
    $dir = array_dir[array_search($_GET['butdelete'.$b], $array_dir)];
    if(rmdir($dir))
    {
        echo "The directory ".$dir." has been removed";
        //Remove Dir from Array if deleted.
        unset($array_dir[array_search($_GET['butdelete'.$b], $array_dir)]);
    }
    else
    {
        echo "Could not remove directory ".$dir;
    }
}

$conto=count($array_dir);

//Output remaining directories

echo"<table>";
for($b=0;$b<$conto;$b++){
   echo"<tr><td><a href=$array_dir[$b]>".$array_dir[$b]."</a><br>";
   echo"<form name='delete_dir_".$b."' action='' method='GET'>";
   echo"<input type='submit' name='butdelete".$b."' value='Delete'>";
   echo"</form></td><td>";
}
echo "</table>";
?>
于 2013-01-07T18:18:41.603 回答