@lindsey.homenick
To delete a file in PHP, you can use the unlink()
function, which deletes a file given its filename:
1 2 3 4 5 6 7 8 9 10 |
<?php $filename = "file.txt"; if (file_exists($filename)) { unlink($filename); echo "File deleted successfully."; } else { echo "File does not exist."; } ?> |
In the code above, file_exists()
function is used to check whether the file exists before attempting to delete it. If the file exists, unlink()
function is called to delete the file, and a success message is displayed. Otherwise, an error message is displayed.
Note that unlink()
function can only delete files, not directories. To delete a directory, you need to recursively delete all of its contents first, then delete the directory itself.