What considerations should be taken into account when storing file names in a database for uploaded images?

When storing file names in a database for uploaded images, it is important to consider security and potential conflicts. To ensure security, sanitize the file names to prevent SQL injection attacks. Additionally, consider adding a unique identifier to the file name to prevent conflicts if multiple users upload files with the same name.

// Sanitize the file name before storing it in the database
$filename = filter_var($_FILES['file']['name'], FILTER_SANITIZE_STRING);

// Generate a unique identifier to append to the file name
$unique_id = uniqid();
$filename = $unique_id . '_' . $filename;

// Store the sanitized and unique file name in the database
$query = "INSERT INTO images (filename) VALUES ('$filename')";
mysqli_query($conn, $query);