delete a file in php

To delete a file in PHP, we can use the unlink() function which deletes the file specified in the parameter passed to it. Here's an example:

main.php
$file_path = "/path/to/file.txt";
if (file_exists($file_path)) {
  unlink($file_path);
  echo "File deleted successfully.";
} else {
  echo "File not found.";
}
161 chars
8 lines

In the code above, we first check if the file exists using file_exists() function. If it exists, then we delete it using unlink() function. If it doesn't exist, we display a message saying that the file was not found.

It's important to note that once the file is deleted, it cannot be recovered. Therefore, it is a good practice to add an extra layer of confirmation before performing this operation.

gistlibby LogSnag