PHP의 배열에서 파일 유형을 확인하십시오.
-
18-09-2019 - |
문제
파일 확장자와 마임 유형이 배열에 있는지 확인하려면 어떻게해야합니다. 이것이 현재 가지고있는 코드입니다.
$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');
그런 다음 파일이 유효한 유형인지 확인하고있는 곳에서 i this foreach loop을 가지고 있습니다.
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;
}
문제는 파일 유형이 배열의 모든 유형이 아닌 경우 (불가능한) 오류가 발생한다는 것입니다. 업로드 파일이 배열에 있으면 오류 메시지가 트리거되지 않는 지점까지 작동합니다.
해결책 3
내가 지금하고있는 방식은 다음과 같습니다.
$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;
}
이점의 장점은 JPEG의 마임이있는 .gif 파일을 허용한다는 것입니다. 따라서 광산과 확장이 일치하도록 강요하지는 않지만 두 이미지 유형인지 확인합니다.
다른 팁
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;
}
이것은 유형과 확장자 모두에서 적절한 문자열을 찾아야합니다.
또 다른 방법은 다음과 같은 루프를 수정하는 것입니다.
$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;
}
파일 배열을 가져 와서 가능한 조건에서 문학적 루프를 사용하는 것을 좋아합니다.
$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);
}
$ msg를 반환합니다.
제휴하지 않습니다 StackOverflow