我有一个file-type
检查image
扩展的验证。
但是,当我尝试上传文件时,例如.exe
or.mp3
以及除允许的扩展名之外的几乎任何内容:
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
它随机工作,我的意思是,有时它会回显错误,有时错误不会被回显。
这是检查扩展名的行.... thingy
if (in_array($image_ext, $allowed_ext) === false){
$errors[] = '<font color="red">*File type is not allowed.</font>';
}
完整代码:
if (isset($_FILES['image'], $_POST['album_id'])){
$image_name = $_FILES['image']['name'];
$image_size = $_FILES['image']['size'];
$image_temp = $_FILES['image']['tmp_name'];
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
//seperate thingies
$tmp = explode('.', $image_name);
$image_ext = strtolower(end($tmp));
$album_id = $_POST['album_id'];
//error array
$errors = array();
if (empty($image_name)){
$errors[] = '<font color="red">*Please choose a photo.</font>';
}
if (empty($album_id)){
$errors[] = '<font color="red">Invalid album.</font>';
} else {
// not allowed extension?
if (!$allowed_ext){
$errors[] = '<font color="red">*The file type is not supported</font>';
}
if (in_array($image_ext, $allowed_ext) === false){
$errors[] = '<font color="red">*File type is not allowed.</font>';
}
// 5 MB file
if ($image_size > 5242880 ){
$errors[] = '<font color="red">*Maximum file size is 2MB.</font>';
}
if (album_check($album_id) === false){
$errors[] = '<font color="red">*Couldn\'t upload to that album.</font>';
}
// puting this in here prevent undefined index error.
$caption = $_POST['caption'];
if (empty($caption)){
$errors[] = '<font color="red">*Caption cannot be empty</font>';
}
}
// check if error, if error, echo errors
if (!empty($errors)){
foreach ($errors as $error){
echo $error, '<br />';
}
} else {
// upload the image if no error
upload_image($image_temp, $image_ext, $album_id);
header('Location: view_album.php?album_id='.$album_id);
exit();
}