What potential pitfalls or errors could occur when using the copy() function for file uploads in PHP?
One potential pitfall when using the copy() function for file uploads in PHP is that it does not handle file name conflicts. If a file with the same name already exists in the destination directory, it will be overwritten without warning. To avoid this issue, you can use the move_uploaded_file() function instead, which automatically appends a number to the file name if a conflict arises.
// Using move_uploaded_file() to handle file uploads with unique file names
$uploadDir = 'uploads/';
$uploadedFile = $_FILES['file']['tmp_name'];
$fileName = $_FILES['file']['name'];
$destination = $uploadDir . $fileName;
if(move_uploaded_file($uploadedFile, $destination)) {
echo "File uploaded successfully.";
} else {
echo "Error uploading file.";
}