How to remove a file from a directory using php

From the name you can see that I am looking for a way to delete a file from another directory. All I can find on this issue is unlink (), but from what I read in the documentation and from testing this function, it removes the file name from the code into which you paste it. It seems to me that it is like closing a file, What I'm trying to do is actually delete the file using the code, so my user does not need to manually go to the folder and find the song that they just deleted from the mysql database.

+4
source share
4 answers

unlink () will delete the file from your server

rmdir () will remove the directory from your server

Note: as soon as it has disappeared, it has disappeared.

+10
source

unlink really removes the specified file from disk

+4
source

We can delete files by specifying our URL or path in PHP using the unlink command. This command will only work if the write permission is specified in the folder or file. Without this, the delete command will not be executed. Here is the delete file command.

$path="images/all11.css"; if(unlink($path)) echo "Deleted file "; 
+3
source
  • realpath - Returns canonicalized absolute pathname
  • is_readable - indicates whether the file exists and is readable
  • unlink - deletes a file

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

+2
source

Source: https://habr.com/ru/post/1445435/


All Articles