Compruebe Subir tipo de archivo de una matriz en PHP.
-
18-09-2019 - |
Pregunta
¿Cómo puedo comprobar si una extensión de archivo y el tipo MIME están en una matriz este es el código que tengo actualmente.
$upload_project_thum = $_FILES['upload_project_thum']['name'];
$upload_project_thum_ext = substr($upload_project_thum, strrpos($upload_project_thum, '.') + 1);
$upload_permitted_types= array('image/jpeg:jpg','image/pjpeg:jpg','image/gif:gif','image/png:png');
A continuación, hacia abajo, donde estoy comprobando si el archivo es un tipo válido tengo este bucle foreach
foreach ($upload_permitted_types as $image_type) {
$type = explode(":", $image_type);
if (($type[0] != $_FILES['upload_project_thum']['type']) && ($upload_project_thum_ext != $type[1]) ) {
$errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail'. $type[1] . " Type: ". $type[0];
$errflag = true;
}
El problema es que si el tipo de archivo no es todos los tipos de la matriz (lo cual es imposible) me da un error. Funciona hasta el punto que si el archivo de carga se encuentra en la matriz que mensaje de error no se disparará.
Solución 3
La forma en que estoy haciendo esto ahora es:
$upload_permitted_types['mime']= array('image/jpeg','image/gif','image/png');
$upload_permitted_types['ext']= array('jpeg','jpg','gif','png');
if(!in_array($_FILES['upload_project_thum']['type'],$upload_permitted_types['mime']) || !in_array($upload_project_thum_ext,$upload_permitted_types['ext'])
{
$errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail';
$errflag = true;
}
La ventaja de esto es que permitirá que un archivo .gif con un mimo de JPEG. Por lo tanto, no obliga a la mina y la extensión de igualar pero no asegurarse de que son los dos tipos de imágenes.
Otros consejos
if (!in_array($_FILES['upload_project_thum']['type'], $upload_permitted_types)){
exit("Unsupported file type");
}
if( !in_array( $_FILES['upload_project_thum']['type'] . ':' . $upload_project_thum_ext, $upload_permitted_types) ) {
Trigger-error-here;
}
Esto debe buscar una cadena adecuada pegado tanto del tipo y extensión.
Otra forma es modificar el bucle de esa manera:
$is_allowed = false;
foreach ($upload_permitted_types as $image_type) {
$type = explode(":", $image_type);
if (($type[0] == $_FILES['upload_project_thum']['type']) && ($type[1] == $upload_project_thum_ext ) ) {
$is_allowed = true;
break;
}
}
if( !$is_allowed ) {
$errmsg_arr[] = 'Please select a jpg, jpeg, gif, or png image to use as the project thumbnail'. $type[1] . " Type: ". $type[0];
$errflag = true;
}
Me gusta llegar la matriz archivos y luego usar un bucle foreach literating sobre posibles condiciones
$is_error = TRUE;
$allowFileTypes = array(
"image/png","image/jpg","image/jpeg"
);
$UserBaseFolder = 'userfiles/'.$_SESSION['user_id'];
if(!file_exists($UserBaseFolder)) {
mkdir("userfiles/".$_SESSION['user_id']."/");
}
if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
foreach($_FILES as $dat => $f) {
if($f['size'] == "0") {
$msg .= '<p><span class="alert alert-danger">You must upload a file.</span></p>';
$is_error = FALSE;
}
if($f['size'] > "2000000") {
$msg .= '<p><span class="alert alert-danger">Your file size is too big.</span></p>';
$is_error = FALSE;
}
if(!in_array($f['type'],$allowFileTypes)){
$msg .= '<p><span class="alert alert-danger">Your file has invalid format. Please try again...</span></p>';
$is_error = FALSE;
} else {
$filepath = $UserBaseFolder.'/'.time().'-'.$f['name'];
move_uploaded_file($f["tmp_name"], $filepath);
}
$ mensaje de retorno;