What are the common pitfalls to avoid when dealing with file names containing special characters in PHP?

When dealing with file names containing special characters in PHP, common pitfalls to avoid include not properly sanitizing user input, not handling encoding properly, and not using the correct functions for working with file names. To solve this issue, it is important to sanitize user input to prevent malicious characters, handle encoding properly to ensure compatibility across different systems, and use functions like `urlencode()` or `rawurlencode()` to encode special characters in file names.

// Sanitize user input for file name
$fileName = preg_replace('/[^\w\._]+/', '', $_POST['fileName']);

// Encode file name for safe use in URLs
$encodedFileName = rawurlencode($fileName);

// Use the encoded file name when working with files
$file = fopen($encodedFileName, 'r');
// Perform file operations...
fclose($file);