Unlink Function in PHP

Last updated 5 years, 9 months ago | 1826 views 75     5

Tags:- PHP

PHP | Unlink() function

The unlink() function is an inbuilt function in PHP that is used to delete the file from a directory. If the file function executes successfully and deletes the file then it returns true otherwise it returns false.


bool unlink ( string $filename [, resource $context ] )


Required. Specifies the file which has to be deleted.


Optional. Specifies the context of the filehandle which can be used to modify the nature of the stream.

$file = "demo.txt";
if (!unlink($file))
  echo ("Error deleting $file");
  echo ($file."file deleted successfully");

Note: For the webserver user must have write permissions to the directory for using the unlink() function.