简体   繁体   中英

how to delete a file from a directory using php

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.

unlink() will delete the file off your server

rmdir() will delete a directory off your server

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

unlink确实从磁盘中删除指定的文件

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 ";
  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM