How can PHP be used to assign unique IDs to each file uploaded through multiple input fields?
When uploading multiple files through input fields in PHP, we can use a combination of a timestamp and a unique identifier to assign a unique ID to each file. This can be achieved by concatenating the current timestamp with a randomly generated string using the uniqid() function. This ensures that each file uploaded will have a distinct identifier.
// Loop through each uploaded file
foreach($_FILES['file']['name'] as $key=>$value){
$timestamp = time(); // Get current timestamp
$unique_id = uniqid(); // Generate unique identifier
$file_id = $timestamp . '_' . $unique_id; // Concatenate timestamp and unique id
// Process the uploaded file with the assigned unique ID
$file_name = $_FILES['file']['name'][$key];
$file_tmp = $_FILES['file']['tmp_name'][$key];
// Move the uploaded file to a specific directory with the unique ID as the filename
move_uploaded_file($file_tmp, 'uploads/' . $file_id . '_' . $file_name);
}