What is the best practice for storing a file name in a variable before submitting a form in PHP?

When storing a file name in a variable before submitting a form in PHP, it is important to ensure that the file name is properly sanitized to prevent any security vulnerabilities such as directory traversal attacks. One way to achieve this is by using the basename() function to extract the file name without the directory path. Additionally, it is recommended to validate the file name to ensure it meets any specific requirements before storing it in a variable.

// Get the file name from the form submission
$file_name = basename($_FILES['file_input']['name']);

// Validate the file name (e.g. check for file extension, length, etc.)
if (/* validation condition */) {
    // Store the file name in a variable for further processing
    $stored_file_name = $file_name;
} else {
    // Handle invalid file name
    echo "Invalid file name.";
}