I have a problem with the unlink() php function. What I try to do is to delete a certain file in a directory. Now the function works fine. But when I call it then it deletes ALL the files inside of that directory. What I want is for it to delete that certain file which the user can select from a list.

I display all the files with php and beside it is a 'X' ahref link which doesn't seem to be identified with that file next to it. This 'X' is used to delete the file.

echo "$file <a title='Delete book' href='#' onclick=".delete($file).">X</a>";

This line prints all the files listed in the directory. (It's in a while loop)

Here is the delete function:

function delete($var){
     $link = './book/'.$var;

I'm not sure how to identify the 'X' with the book next to it. Because at the moment it seems 'X' is linked to all the files.

Any help will be appreciated!


Recommended Answers

All 2 Replies

        echo "$file <a title='Delete book' href='Page-name.php?del=$file'>X</a>";
        if(isset($_REQUEST['del'])) {
             $link = './book/'.$_REQUEST['del'];
             if(unlink($link)) echo "Success";
             else echo "Failed";

Thank you so much! That worked 100%!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.