PHP | unlink() function

The unlink() function in PHP is an inbuilt function which is used to delete a file. It is similar to the unlink() function found on Unix.
The filename of the file which has to be deleted is sent as a parameter and the function returns True on success and False on failure.

Syntax:

unlink(filename, context)

Parameters Used:
The unlink() function in PHP accepts two parameter.

  1. filename : It is a mandatory parameter which specifies the filename of the file which has to be deleted.
  2. context : It is an 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

  1. The unkink() function generates an E_WARNING level error on failure.
  2. The web server user must have write permissions to the directory for using the unlink() function.
  3. The unlink() function returns Boolean False but many times it happens that it returns a non-Boolean value which evaluates to False.

Examples:

Input : $file_pointer = "gfg.txt";
        if (!unlink($file_pointer))
        {
           echo ("$file_pointer cannot be deleted due to an error");
        }
        else
        {
           echo ("$file_pointer has been deleted");
        }

Output : 1

Input : $file_pointer = fopen('gfg.txt');
        fwrite($file_pointer, 'A computer science portal for geeks!');
        fclose($file_pointer);
        unlink('gfg.txt');

Output : 1

Below programs illustrate the unlink() function.

Suppose there is a file named “gfg.txt”

Program 1

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to delete a file named gfg.txt 
// using unlike() function
  
$file_pointer = "gfg.txt";
  
// using unlink() function to delete a file
if (!unlink($file_pointer))
  {
  echo ("$file_pointer cannot be deleted due to an error");
  }
else
  {
  echo ("$file_pointer has been deleted");
  }
?>

chevron_right


Output:

1
filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to delete a file named gfg.txt 
// using unlike() function
  
$file_pointer = fopen('gfg.txt');
  
// writing on a file named gfg.txt
fwrite($file_pointer, 'A computer science portal for geeks!');
fclose($file_pointer);
  
// using unlink() function to delete a file
unlink('gfg.txt');
  
?>

chevron_right


Output:

1

Reference:
http://php.net/manual/en/function.unlink.php



My Personal Notes arrow_drop_up

Image
I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.