PHP unlink () function

PHP

Syntax:
unlink ($filename, $context) 
Parameters:this function takes two parameters mentioned above and described below:
  • $filename:is a required parameter and specifies the name of the file to be deleted.
  • $context :This is an optional parameter that defines the context of the file descriptor that can be used to change the nature of the stream.
Return Value:returns True on success and False on failure.Errors and exceptions:
  • The unkink() function generates an E_WARNING level error on failure.
  • The web server user must have write access to the directory to use the unlink() function.
  • The unlink() function returns Boolean False, but it often happens that it returns a non-boolean value that evaluates to False .
Below I illustrate the program t unlink() function in PHP:Suppose there is a file named gfg.txt Program 1:
// PHP program to delete a file named gfg.txt
// using the different() function  $file_pointer = "gfg.txt" ;  
// Use the unlink() function to delete the file if (! unlink ( $file_pointer )) { echo ( "$file_pointer cannot be deleted due to an error" ); } else { echo ( "$file_pointer has been deleted" ); }  
?>
Output:
gfg.txt has been deleted 
Program 2:
// PHP program to delete a file named gfg.txt
// using the different() function $file_pointer = fopen ( ' gfg.txt' , 'w +' );  
// write to a file named gfg.txt fwrite ( $file_pointer , 'A computer science portal for engineer!' ); fclose ( $file_pointer );  
// Use the unlink() function to delete the file if (! unlink ( $file_pointer )) { echo ( "$file_pointer cannot be deleted due to an error" ); } else { echo ( "$file_pointer has been deleted" ); }  
?>
Output:
Warning: unlink() expects parameter 1 to be a valid path, resource given in C: xampphtdocsserver.php on line 12 Resource id # 3 cannot be deleted due to an error 
Link: http://php.net/manual/en/function.unlink.php



Get Solution for free from DataCamp guru