Are there any best practices for handling file names in PHP to avoid errors or security vulnerabilities?

When handling file names in PHP, it is important to sanitize and validate user input to prevent errors and security vulnerabilities. One common practice is to use functions like `basename()` to extract the file name from a path and remove any potentially harmful characters. Additionally, it is recommended to limit the allowed characters in file names to alphanumeric characters, dashes, and underscores to avoid any potential security risks.

// Example of sanitizing a file name in PHP
$unsafeFileName = $_POST['file_name']; // User input
$cleanFileName = preg_replace("/[^a-zA-Z0-9-_\.]/", "", $unsafeFileName);
$cleanFileName = basename($cleanFileName);