<?php

if (!function_exists('devsaccounting_validate_file')) {
    function devsaccounting_validate_file($file) {
        // Initialize an error message variable
        $errors = [];
 
        // Validate and sanitize the file
        if (empty($file) || !isset($file)) {
            $errors['error_msg'] = esc_html__('No file provided.', 'devs-accounting');
        }
 
        // Check file type
        $allowed_types = array('jpg', 'jpeg', 'png', 'gif');
        $file_type = wp_check_filetype_and_ext($file['tmp_name'], $file['name']);
       //  $errors['error_msg'] = $file_type;
        if (!in_array($file_type['ext'], $allowed_types) || !$file_type['type']) {
          $errors['error_msg'] = esc_html__('Invalid file type.', 'devs-accounting');
        }
 
        // If there's an error message, return it
        if (!empty($errors)) {
            return $errors;
        }
        return $file;
        
    }
 }