#53: Deleting Files

To remove a file on Unix, use the unlink() function:

<?
if (unlink("file.txt")) {
    echo "file.txt deleted.";
} else {
    echo "file.txt: delete failed.";
}
?>

You need the proper permissions to delete a file, of course. However, the biggest danger here is that you can delete a file that you didn't mean to delete. When you delete a file in Unix, it's gone; there's no undelete or trash can. The only way you might get something back is from your system administrator's backups.

And be careful here if you plan to have user actions deleting files. This is dangerous for every reason discussed and for every other file operation discussed so far.

Get Wicked Cool PHP now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.