Trying to make a dropdown list, displaying files from a specific folder, with a Delete button to delete the file selected.
Dropdown list:
<?php
$dirname = "files";
$dir = opendir($dirname);
echo '<form action="delete.php" method="get">';
echo '<select name="file2">';
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != ".."))
{
echo "<option value=".$file.">$file</option>";
}
}
echo '</select>';
echo '<input type="submit" value="Delete" class="submit" />';
echo '</form>';
?>
the delete.php file:
<?php
$dirpath = "files";
$file_to_delete = $_POST['file2'];
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
} else {
echo "Error.";
}
?>
When I then try to selected a file and press delete, I get the following error:
Warning: unlink(files/): Is a directory in /xxx/xxx/xxx/xxx/xxx/xxx/xxx/xxx/delete.php on line 4 Error.
xxx'ed out due to privacy :) all the files trying to be deleted are chmod 777. its a simply .txt file I'm trying to delete.
Not sure what I'm missing or what I did wrong here... :/
It's $_GET
, because your form has method="get"
:
$file_to_delete = $_GET['file2'];
so:
<?php
$dirpath = "files";
$file_to_delete = $_GET['file2'];
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
echo $file_to_delete . " deleted.";
} else {
echo "Error.";
}
?>
If you want post change your form to method="post"
.