4

From the title you can see i am looking for a way to delete a file from a different directory. All i can find on the subject is the unlink(), but from what i read in the documentation and from testing that function is it deletes the file name from the code you put it in. Makes me think it's quite similar to closing the file. What I am trying to do is actually delete a file using code so my user doesn't have to manually go to the folder and find the song they just deleted from the mysql database.

2

4 Answers 4

13

unlink() will delete the file off your server

rmdir() will delete a directory off your server

Note: Once it's gone, it's gone.

Sign up to request clarification or add additional context in comments.

4 Comments

that's what i keep reading but when i use unlink() it doesn't follow the path. the error says the file or directory does not exist in the file i'm working with. it doesn't go ../songUploads/*file*. it goes "file or directory does not exist in html/songDelete.php". which leads me to think it's looking for the file i want to delete in the file that the unlink() exists which makes no sense to me.
try starting from the root directory $_SERVER['DOCUMENT_ROOT'] then using the relative path
that did it. any idea why the normal path syntax doesn't work?
I'm not sure, but I found this that seems to be related php.net/manual/en/function.unlink.php#80110
4

unlink truly deletes the specified file from the disk

Comments

3

We can delete files by giving its URL or path in PHP by using unlink command. This command will work only if write permission is given to the folder or file. Without this the delete command will fail. Here is the command to delete the file.

$path="images/all11.css"; if(unlink($path)) echo "Deleted file "; 

Comments

2
  • realpath — Returns canonicalized absolute pathname
  • is_readable — Tells whether a file exists and is readable
  • unlink — Deletes a file

Run your filepath through realpath, then check if the returned path exists and if so, unlink it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.