check file type and size (PHP)
wptutor
2024-08-31
17 Views
function check_file_type($file) {
//$allowed_extensions = ['jpg','jpeg','png','gif','pdf','docx'];
//$allowed_fileTypes = ['image/jpeg','image/png','image/gif','application/pdf','application/msword'];
$allowed_types = array('image/jpeg','image/png','application/pdf','application/msword','application/vnd.ms-excel','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet','text/csv');
if(!in_array($file['type'],$allowed_types)) {
wp_send_json_error(array('message' => 'Invalid file type,please upload jpeg\png\pdf\winword'));
}
}
function check_file_size($file) {
$max_size = 5*1024*1024;
if($file['size'] > $max_size) {
wp_send_json_error(array('message' => 'File sieze exceeds the maximum limit of 5m'));
}
}