PHP unlink files after read them

1k views Asked by At

I read files in a folder.

Now I want to have each file with a "edit", "start" and "delete" button.

The problem is that when I click "delete", PHP will delete the last file read, instead of the file where I click "delete"

I understand that it's the While loop, that does not give any information to the unlink function, but I don't get it right.

Thanks for help! :)

<?php 
if ($handle = opendir('rezepte/')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            #Entfernt der Datei das ".txt"
            $replacer = str_replace(".txt","",$entry);
            echo "
                <div class='eintrag'>$replacer</div><br>
                <a href='$replacer.txt'>edit</a>
                <a href='$replacer'> start</a>

                <form method='post'>
                    <input type='submit' name='delete' value='delete'/>
                </form>";       
        }       
    }
    closedir($handle);
}   

if(isset($_POST['delete'])) {
    echo "
        <div class='warning'>";
    echo $replacer;
    echo "Do you really want to delete?>
        <form method='post'>
            <input type='submit' name='really_delete' value='delete it'/>
        </form>

        <form method='post'>
            <input type='submit' name='not_delete' value='cancel'/>
        </form></div>";
}   

if(isset($_POST['really_delete'])) {
    unlink("rezepte/".$replacer.".txt");
}

if(isset($_POST['not_delete'])) {
}

?>

2

There are 2 answers

0
Codemole On BEST ANSWER

Well, according to your coding style, you must make coding like following:

<?php 

if(isset($_POST['delete'])) {
    $replacer = $_POST['delete'];
    echo "
        <div class='warning'>";
    echo $replacer;
    echo "Do you really want to delete?>
        <form method='post'>
            <input type='submit' name='really_delete' value='$replacer'/>
        </form>

        <form method='post'>
            <input type='submit' name='not_delete' value='cancel'/>
        </form></div>";
}   

if(isset($_POST['really_delete'])) {
    $replacer = $_POST['really_delete'];
    unlink("rezepte/".$replacer.".txt");
}

if(isset($_POST['not_delete'])) {
}

if ($handle = opendir('rezepte/')) {
    while (false !== ($entry = readdir($handle))) {
        if ($entry != "." && $entry != "..") {
            #Entfernt der Datei das ".txt"
            $replacer = str_replace(".txt","",$entry);
            echo "
                <div class='eintrag'>$replacer</div><br>
                <a href='$replacer.txt'>edit</a>
                <a href='$replacer'> start</a>

                <form method='post'>
                    <input type='submit' name='delete' value='$replacer'/>
                </form>";       
        }       
    }
    closedir($handle);
}   
2
chris g On

Ignoring some of the syntax errors in the code above, you could place the file name ($replacer) into the value for the delete submit button like so:

<input type='submit' name='delete' value='" . $replacer . "'/>

Which would send the value of $replacer right where you appear to want it.

Ideally, though, it would make more sense to put a hidden form field with the name in it like so:

'<input type="hidden" name="filename" value="' . $replacer . '"/>'

And change your form processor to look for $_POST['filename'] instead.