How can you dynamically generate a new file name for uploaded images in PHP?

When uploading images in PHP, it is important to generate a unique file name for each image to avoid overwriting existing files. One way to dynamically generate a new file name is to use a combination of a timestamp and a random string to ensure uniqueness.

// Generate a unique file name for uploaded images
$timestamp = time();
$random_string = bin2hex(random_bytes(5));
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
$new_filename = $timestamp . '_' . $random_string . '.' . $extension;

// Move the uploaded file to a new location with the generated file name
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $new_filename);