How can PHP developers protect against SQL injection when interacting with databases for file management in web applications?

To protect against SQL injection when interacting with databases for file management in web applications, PHP developers should use prepared statements or parameterized queries. This helps to ensure that user input is properly sanitized and treated as data rather than executable SQL code.

// Example of using prepared statements to protect against SQL injection

// Establish a connection to the database
$pdo = new PDO('mysql:host=localhost;dbname=files', 'username', 'password');

// Prepare a SQL statement with a placeholder for user input
$stmt = $pdo->prepare('SELECT * FROM files WHERE filename = :filename');

// Bind the user input to the placeholder
$stmt->bindParam(':filename', $_GET['filename']);

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

// Fetch the results
$results = $stmt->fetchAll();