What are the best practices for handling file uploads and descriptions in PHP when faced with server limitations?

When faced with server limitations for handling file uploads and descriptions in PHP, it is essential to optimize the code to minimize resource usage. This can be achieved by setting appropriate limits for file size, validating file types, and properly sanitizing user input for descriptions to prevent security vulnerabilities.

// Limit the file size to 5MB
define('MAX_FILE_SIZE', 5 * 1024 * 1024); 

// Allowed file types
$allowed_types = array('jpg', 'jpeg', 'png', 'gif');

// Sanitize and validate the file description
$description = filter_var($_POST['description'], FILTER_SANITIZE_STRING);

// Check if file size exceeds the limit
if ($_FILES['file']['size'] > MAX_FILE_SIZE) {
    echo 'File size exceeds the limit';
}

// Check if file type is allowed
$file_ext = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if (!in_array($file_ext, $allowed_types)) {
    echo 'Invalid file type';
}

// Process the file upload
// Add your code here to handle the file upload