How can PHP be used to prevent files from being overwritten during upload?

To prevent files from being overwritten during upload in PHP, one approach is to generate a unique filename for each uploaded file. This can be achieved by appending a timestamp or a random string to the original filename before saving it to the server.

// Generate a unique filename by appending a timestamp to the original filename
$uploadedFile = $_FILES['file']['name'];
$extension = pathinfo($uploadedFile, PATHINFO_EXTENSION);
$uniqueFilename = time() . '_' . uniqid() . '.' . $extension;

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