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.
4 Answers
unlink() will delete the file off your server
rmdir() will delete a directory off your server
Note: Once it's gone, it's gone.
4 Comments
Jon
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.
Samuel Cook
try starting from the root directory
$_SERVER['DOCUMENT_ROOT'] then using the relative pathJon
that did it. any idea why the normal path syntax doesn't work?
Samuel Cook
I'm not sure, but I found this that seems to be related php.net/manual/en/function.unlink.php#80110
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
- 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.