To delete a file by using PHP is very easy. Deleting a file means completely erase a file from a directory so that the file is no longer exist. PHP has an unlink() function that allows to delete a file. The PHP unlink() function takes two parameters $filename and $context.
unlink( $filename, $context );
Below programs illustrate the above approach:
Program 1: This program uses unlink() function to remove file from directory.
Suppose there is a file named as “gfg.txt”
gfg.txt has been deleted
Program 2: This program uses unlink() function to delete a file from folder after using some operation.
Warning: unlink() expects parameter 1 to be a valid path, resource given in C:\xampp\htdocs\server.php on line 12 Resource id #3 cannot be deleted due to an error
Note: If the file does not exist then it will display an error.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.