Is it recommended to use classes and objects when working with file uploads in PHP, and why?

When working with file uploads in PHP, it is recommended to use classes and objects to encapsulate the logic and functionality related to handling file uploads. This approach helps in organizing the code, making it more modular, reusable, and easier to maintain. By using classes and objects, you can create custom upload handlers, validate file types and sizes, handle errors, and securely save the uploaded files to the server.

<?php

class FileUploader {
    private $uploadDir;

    public function __construct($uploadDir) {
        $this->uploadDir = $uploadDir;
    }

    public function uploadFile($fileInputName) {
        $uploadedFile = $_FILES[$fileInputName];
        $targetPath = $this->uploadDir . basename($uploadedFile["name"]);

        if (move_uploaded_file($uploadedFile["tmp_name"], $targetPath)) {
            echo "File uploaded successfully.";
        } else {
            echo "Error uploading file.";
        }
    }
}

$uploader = new FileUploader("uploads/");
$uploader->uploadFile("file_input_name");

?>