How to delete a file selected from a dropdown List.

I have a dropdown list populated with files pulled from a directory using the PHP listed below then displayed on a page in textareas and am trying to figure out how I can delete them using a delete button in the form when they're selected.

Below is the list of your saved codes. To edit your codes, select it from the list.

<?php $directory = $directory = 'users/' . $_SESSION['username']; $filesContents = Array(); $files = scandir( $directory ) ; foreach( $files as $file ) { if ( ! is_dir( $file ) ) { $filesContents[$file] = file_get_contents($directory , $file); echo "<option>" . $file . ""; } } ?>

delete php script.

<?php session_start(); $directory = $directory = $_SERVER['DOCUMENT_ROOT'] . '/users/' . $_SESSION['username']; $file_to_delete = $_POST['CodeList']; if ( unlink ($directory.'/'.$file_to_delete) ) { echo $file_to_delete . " deleted."; } else { echo "Error."; } ?>

when I try and save I get the Warning: unlink(/myhome/root/public_html/users/Addiction/) [function.unlink]: No such file or directory in /home/revo/public_html/evo/avdeleteprocess.php on line 5 Error.

So its found the right folder, just not finding the file from the dropdown list selection.

Also edited to add I ran a var_dump($_POST) right after my delete script to see what it was pulling as the value of POST and it came back with: Error.array(1) { ["Action"]=> string(6) "Delete" }


  • edited July 2015

    $directory = $directory = 'users/' . $_SESSION['username'];
    $filesContents = Array();
    $files = scandir($directory);
    foreach ($files as $file) {
        if (!is_dir($file)) {
            $filesContents[$file] = file_get_contents($directory , $file);
            echo "<option>" . $file . "";

    Is that a typo? $directory is going to be a Boolean. Same with the other code, too.

    Did you mean for the comma while getting the file contents to be a period? :smiley:

    I presume some code was removed because of the empty quotes appended at the end of the 'option' echo...?

    $directory = $directory = $_SERVER['DOCUMENT_ROOT'] . '/users/' . $_SESSION['username'];
    $file_to_delete = $_POST['CodeList'];
    if (unlink($directory.'/'.$file_to_delete)) {
        echo $file_to_delete." deleted.";
    } else {
        echo "Error.";

    As far as the file from the drop down is concerned, it looks like you are not sending it with the request. The code you shared does not include that part.

    You may want to consider checking the file before deleting it though... I may be able to send "../../../whatever.php" and make you delete a script. Just a suggestion. Sanitize EVERYTHING you use in your application logic...

    Your Select Tags should prolly be formatted something like this:

    <form method='post'>
        <select name='CodeList'>
            <option value='file1.txt'>file1.txt</option>
            <option value='file2.txt'>file2.txt</option>
            <option value='file3.txt'>file3.txt</option>
        <input type='submit' name='Action' value='Delete' />

    Hope that helps.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


In this Discussion