What best practices should PHP developers follow to prevent file overwrite issues during uploads?
File overwrite issues during uploads can be prevented by generating 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. By ensuring that each file has a unique name, developers can avoid accidentally overwriting existing files.
// Generate a unique filename by appending a timestamp
$uploadedFile = $_FILES['file']['name'];
$extension = pathinfo($uploadedFile, PATHINFO_EXTENSION);
$uniqueFilename = time() . '_' . uniqid() . '.' . $extension;
// Move the uploaded file to a specific directory with the unique filename
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $uniqueFilename);