I have a form that allows users to upload an image. I'm using the code below (which currently doesn't work) to ensure what a user uploads is valid.
Can someone help me out and either update my code or point me in the right direction as to how I can use as many of the built-in WordPress functions (wp_check_filetype_and_ext()), or anything else that would be well-suited, to handle the validation as possible? It also must be secure to prevent any malicious files, attacks, etc.
if ( $_FILES ) {
foreach ($_FILES as $file => $array) {
//Check if the $_FILES is set and if the size is > 0 (if =0 it's empty)
if ( isset( $_FILES[$file]) && ($_FILES[$file]['size'] > 0 ) ) {
$tmpName = $_FILES[$file]['tmp_name'];
list($width, $height, $type, $attr) = @getimagesize($tmpName);
if ($width != 500 || $height != 500) {
$error .= "Image is to small<br />";
unlink($_FILES[$file]['tmp_name']);
}
// Get the type of the uploaded file. This is returned as "type/extension"
$arr_file_type = wp_check_filetype(basename($_FILES[$file]['name']));
$uploaded_file_type = $arr_file_type['type'];
// Set an array containing a list of acceptable formats
$allowed_file_types = array('image/jpg','image/jpeg','image/gif','image/png');
// If the uploaded file is the right format
if (in_array($uploaded_file_type, $allowed_file_types)) {
} else { // wrong file type
$error .= "Please upload a JPG, GIF, or PNG file<br />";
}
}
}
}
media_handle_upload
instead?