How can the issue of duplicate entries be resolved in PHP when uploading images?

When uploading images in PHP, one way to resolve the issue of duplicate entries is to generate a unique filename for each image before saving it to the server. This can be achieved by appending a timestamp or a random string to the original filename. By ensuring that each image has a distinct filename, you can prevent duplicate entries in your database.

// Generate a unique filename for the uploaded image
$originalFilename = $_FILES['image']['name'];
$extension = pathinfo($originalFilename, PATHINFO_EXTENSION);
$uniqueFilename = uniqid() . '_' . $originalFilename;

// Move the uploaded image to a specified directory with the unique filename
move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $uniqueFilename);

// Save the unique filename to the database instead of the original filename
$query = "INSERT INTO images (filename) VALUES ('$uniqueFilename')";
// Execute the query and handle any errors