Unlink() function: unlink() function - it is a built-in function in PHP that is used to delete a file. The filename of the file to be deleted is sent as a parameter, and the function returns True on success and False on error. The unlink() function in PHP takes two parameters.
Syntax:unlink (filename, context)
Parameters :This function takes two parameters as above and described below:
- filename:This is a required parameter and specifies the name of the file to be deleted.
- context:this is an optional parameter that specifies a file descriptor context that can be used to change the nature of the stream.
Return value :returns True on success and False on failure.Suppose there is a file named
’gfg.txt’Example :
// PHP program to delete a file named gfg .txt
// using the different() function
$file_pointer
=
fopen
(
’gfg.txt’
);
// Write to file named gfg.txt
fwrite (
$file_pointer
,
’A computer science portal for engineer!’
);
fclose (
$file_pointer
);
// Using the unlink() function to delete a file
unlink (
’gfg.txt’
);
?>
Output:1
Note.If we do not have permission to access the "gfg.txt" file, the unkink() function generates an E_WARNING level error on error. Unset() function: Unset() function - it is a built-in function in PHP that is used to remove content from a file by cleaning it up. This means that the function cleans up the contents of the file, but rather deletes it. The unset() function not only clears the contents of a file, but is also used to reset a variable, making it empty.Syntax:unset ($variable)
Parameter:This function accepts a variable withone parameter, which is required. This is a variable that must be reset.Returned value:This function does not return any value.Example:
$var
=
"hello"
;
// The change will be reflected outside the function
function
unset_value() {
unset (
$GLOBALS
[
’ var’
]);
}
unset_value();
echo
$var
;
?>
Output:No output
Difference between unlink() and unset() functions: unlink() Function unset() Function It is used to delete a file within a directory completely on successful execution. It is used to make a specific file empty by removing its content. There are two parameter filenameand the other one is context . There is only one parameter variable. Return True on success and false on failure. This function does not return any value. This is a function for file system handling. This is a function for variable management.