What are the best practices for checking the length of a filename in PHP before performing operations like unlink?

When working with filenames in PHP, it is important to check the length of the filename before performing operations like unlink to prevent potential errors or security vulnerabilities. One way to do this is by using the strlen function to get the length of the filename and comparing it against a maximum allowed length. If the filename exceeds the maximum length, you can handle the error accordingly.

$filename = "example_filename.txt";
$max_length = 255; // maximum allowed filename length

if(strlen($filename) > $max_length){
    echo "Error: Filename exceeds maximum allowed length.";
} else {
    // perform operations like unlink
    unlink($filename);
}