How can PHP scripts be optimized to assign unique names to uploaded images, such as "datei1" for the first upload, to prevent naming conflicts and improve organization?

To assign unique names to uploaded images in PHP, you can use a combination of a base name (e.g., "image") and a unique identifier (e.g., timestamp or a random string) to prevent naming conflicts and improve organization. This can be achieved by appending the unique identifier to the base name before saving the file.

// Generate a unique identifier
$unique_id = uniqid();

// Get the original file name
$original_name = $_FILES['file']['name'];

// Extract file extension
$extension = pathinfo($original_name, PATHINFO_EXTENSION);

// Construct a unique file name
$new_file_name = 'image_' . $unique_id . '.' . $extension;

// Move the uploaded file to a directory with the unique name
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $new_file_name);