Delete from dropdown box

Here’s a really cool script that lets you delete files from a folder using a dropdown box
Background script:

<?php
$dirpath = "../gallery/";
$file_to_delete = $_GET['file2'];
if ( unlink ($dirpath.'/'.$file_to_delete) ) {
    echo $file_to_delete . " deleted.";
} else {
    echo "Error.";
}?>

Front end script:

<?php
$dirname = "../gallery/";
$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>';?>

Hope this helps someone

Thanks for that. Unless it’s the way the forum presented it, I think you’d want to add some quotes in this line though, as otherwise it might cause problems when you present a filename that contains spaces.

        echo "<option value=".$file.">$file</option>";

Perhaps

echo '<option value="' . $file . '">' . $file . '</option>';

using single quotes so you can put double-quotes inside the html without escaping it.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.