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.
- How to delete text from file using preg_replace() function in PHP ?
- How to get file input by selected file name without path using jQuery ?
- PHP | Program to delete an element from array using unset() function
- How to reset selected file with input tag file type in Angular 9?
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | MySQL Delete Query
- How to delete an array element based on key in PHP?
- How to recursively delete a directory and its entire contents (files + sub dirs) in PHP?
- How to move a file into a different folder on the server using PHP?
- Download file from URL using PHP
- How to copy a file from one directory to another using PHP ?
- How to make PDF file downloadable in HTML link using PHP ?
- How to generate an XML file dynamically using PHP?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.