What are some best practices for handling file names and extensions in PHP when writing them to a TXT file?

When writing file names and extensions to a TXT file in PHP, it is important to sanitize the input to prevent any malicious code injection or invalid characters. One way to do this is by using the `basename()` function to extract the file name and extension from the input and then writing it to the TXT file. Additionally, you can use `pathinfo()` function to validate the file extension before writing it to the file.

// Sanitize file name and extension before writing to TXT file
$filename = "example.php"; // Example file name with extension

// Extract file name and extension
$filename = basename($filename);
$extension = pathinfo($filename, PATHINFO_EXTENSION);

// Validate file extension
if($extension !== 'txt'){
    echo "Invalid file extension";
} else {
    // Write file name and extension to TXT file
    $txtFile = fopen("output.txt", "w");
    fwrite($txtFile, $filename);
    fclose($txtFile);
}