What are some potential pitfalls when generating and naming files in PHP for a website?

One potential pitfall when generating and naming files in PHP for a website is not properly sanitizing user input, which can lead to security vulnerabilities such as directory traversal attacks. To mitigate this risk, always validate and sanitize user input before using it to create or name files.

// Sanitize user input before generating or naming files
$user_input = $_POST['user_input'];
$clean_input = filter_var($user_input, FILTER_SANITIZE_STRING);

// Use the sanitized input to generate or name files
$file_name = 'file_' . $clean_input . '.txt';