我有一个包含大约10个字段的表单,最后一个是imageUpload字段,它是不是必填字段。我只是在填充字段时尝试对文件进行exif检查。但是正如标题所述!在检查是否填充时,empty会失败并且它会继续,就像那里有一个文件一样,这当然会引发警告。 Warning: exif_imagetype(): Filename cannot be empty in
if(!empty($_FILES['imageUpload'])){
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['imageUpload']['tmp_name']);
$isImage = !in_array($detectedType, $allowedTypes);
if(!empty($isImage)){
$errormsg[] = 'This is not a valid image file.';
}
}
这是该字段的var_dump。 array(1) { ["imageUpload"]=> array(5) { ["name"]=> string(0) "" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(4) ["size"]=> int(0) } }
那么我做错了什么?
答案 0 :(得分:2)
请尝试以下代码:
if (!empty($_FILES['imageUpload']['tmp_name'])){
$allowedTypes = array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['imageUpload']['tmp_name']);
$isImage = !in_array($detectedType, $allowedTypes);
if(!empty($isImage)){
$errormsg[] = 'This is not a valid image file.';
}
}
答案 1 :(得分:0)
检查文件的tmp_name
:
if (!empty($_FILES['imageUpload']['tmp_name'])) {
// do stuff
}