What is the purpose of the php unlink function?
The unlink() function in PHP is a built-in function used to delete files. It takes one parameter, which is the file path to be deleted.
The function permanently deletes a file at the specified path from the file system. It returns true if the file exists and is successfully deleted, and false if the file does not exist or deletion fails.
Example of use:
$file = 'path/to/file.txt';
if (file_exists($file)) {
if (unlink($file)) {
echo "文件删除成功";
} else {
echo "文件删除失败";
}
} else {
echo "文件不存在";
}
In the example above, the file existence is checked first using the file_exists() function. If the file exists, the unlink() function is then called to delete the file, and a corresponding message is output based on the return result.
It is important to note that the unlink() function cannot be undone after performing the deletion operation, so caution should be taken to confirm the files that are to be deleted. To avoid accidentally deleting important files, it is recommended to perform necessary validation and permission control before calling the unlink() function.