How can file uploads in PHP be associated with unique IDs in a database?

When a file is uploaded in PHP, it can be associated with a unique ID in a database by generating a unique identifier for each file and storing it in the database along with the file details. This unique ID can be a randomly generated string or a numeric value that is unique for each file upload. By linking the file details with this unique ID in the database, you can easily retrieve and manage the uploaded files.

// Generate a unique ID for the file upload
$unique_id = uniqid();

// Store the file details and unique ID in the database
$query = "INSERT INTO files (file_name, unique_id) VALUES ('$file_name', '$unique_id')";
// Execute the query to store the file details in the database

// Retrieve the file using the unique ID
$unique_id = $_GET['unique_id'];
$query = "SELECT * FROM files WHERE unique_id = '$unique_id'";
// Execute the query to retrieve the file details from the database