How can user-specific identifiers be incorporated into file names to avoid conflicts in a shared directory for uploaded images?

To avoid conflicts in a shared directory for uploaded images, user-specific identifiers can be incorporated into file names. This can be achieved by appending the user's unique identifier (such as their user ID or username) to the file name before saving it in the directory. This ensures that each uploaded image has a distinct file name, preventing conflicts when multiple users upload files with the same name.

$user_id = 123; // User's unique identifier
$file_name = $_FILES['image']['name']; // Original file name
$extension = pathinfo($file_name, PATHINFO_EXTENSION); // Get file extension

$new_file_name = $user_id . '_' . uniqid() . '.' . $extension; // Append user ID and unique identifier to file name

move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/' . $new_file_name); // Save uploaded image with unique file name