Home >Backend Development >PHP Tutorial >PHP file deletion method_PHP tutorial
The unlink() function deletes files.
Returns true if successful, false if failed.
Syntax
unlink(filename,context)
Parameter Description
filename Required. Specifies the files to be deleted.
context optional. Specifies the environment for a file handle. Context is a set of options that modify the behavior of the stream.
$file_delete = "home/meeta/my.php";
if (unlink($file_delete)) {
echo "The file was deleted successfully. ", "n";
} else {
echo "The specified file could not be deleted. Please try again.", "n";
}
?>The judgment file is added below Does it exist
$myfile = "./test1.txt";
if (file_exists($myfile)) {
$result=unlink ($myfile);
echo $ result;
}
?>
file_exists() function checks whether the file or directory exists.
Returns true if the specified file or directory exists, false otherwise.
Syntax
file_exists(path)