The unlink() function is an inbuilt function in PHP which is used to delete files. It is similar to UNIX unlink() function. The $filename is sent as a parameter that needs to be deleted and the function returns True on success and false on failure.
unlink( $filename, $context )
Parameters: This function accepts two parameters as mentioned above and descrined below:
- $filename: It is mandatory parameter which specifies the filename of the file which has to be deleted.
- $context: It is optional parameter which specifies the context of the file handle which can be used to modify the nature of the stream.
Return Value: It returns True on success and False on failure.
Errors And Exception:
- The unlink() function generates an E_WARNING level error on failure.
- The web server user must have write permissions to the directory for using the unlink() function.
- The unlink() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.
Below programs illustrate the unlink() function in PHP:
Suppose there is a file named as “gfg.txt”
gfg.txt has been deleted
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
- PHP | Unset() vs Unlink() Function
- Node.js | fs.unlink() Method
- How to get the function name inside a function in PHP ?
- PHP 5 vs PHP 7
- PHP | Get PHP configuration information using phpinfo()
- PHP | php.ini File Configuration
- How to import config.php file in a PHP script ?
- PHP | imagecreatetruecolor() Function
- PHP | fpassthru( ) Function
- PHP | ImagickDraw getTextAlignment() Function
- PHP | Ds\Sequence last() Function
- PHP | Imagick floodFillPaintImage() Function
- Function to escape regex patterns before applied in PHP
- PHP | array_udiff_uassoc() Function
- PHP | geoip_continent_code_by_name() Function
- PHP | GmagickPixel setcolor() function
- PHP | opendir() Function
- PHP | cal_to_jd() Function
- PHP | stream_get_transports() Function
- PHP | Ds\Deque pop() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
Improved By : iamabhishek