Search code examples
phpfiledelete-fileunlink

PHP unlink files after read them


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'])) {
}

?>


Solution

  • 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);
    }