Are there any best practices to follow when working with file names and extensions in PHP?

When working with file names and extensions in PHP, it is important to follow best practices to ensure compatibility and security. One common best practice is to sanitize user input to prevent any malicious code injection. Additionally, using functions like pathinfo() can help extract file extensions safely.

// Sanitize user input for file name
$fileName = filter_var($_POST['fileName'], FILTER_SANITIZE_STRING);

// Get file extension using pathinfo()
$extension = pathinfo($fileName, PATHINFO_EXTENSION);