What potential pitfalls should be considered when storing file names in a database for image display in PHP?

One potential pitfall to consider when storing file names in a database for image display in PHP is the risk of SQL injection attacks if the file names are not properly sanitized before being inserted into the database. To mitigate this risk, always use prepared statements with parameterized queries when inserting file names into the database.

// Connect to database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');

// Prepare SQL statement with a placeholder for the file name
$stmt = $pdo->prepare("INSERT INTO images (file_name) VALUES (:file_name)");

// Bind the file name parameter to the placeholder
$stmt->bindParam(':file_name', $file_name);

// Sanitize the file name before inserting into the database
$file_name = filter_var($file_name, FILTER_SANITIZE_STRING);

// Execute the SQL statement
$stmt->execute();