我已包含文件类型,请检查以下代码。如果我从允许的类型中选择2个文件,从不允许的类型中选择一个文件,则代码仍会保存允许的一个,并显示文件类型的msg。我想如果文件不是允许的文件,则代码不应保存任何内容。
<?php
// Script written by Adam Khoury for the following video exercise:
// http://www.youtube.com/watch?v=7fTsf80RJ5w
if(isset($_FILES['file_array'])) {
$name_array = $_FILES['file_array']['name'];
$tmp_name_array = $_FILES['file_array']['tmp_name'];
$type_array = $_FILES['file_array']['type'];
$size_array = $_FILES['file_array']['size'];
$error_array = $_FILES['file_array']['error'];
for($i = 0; $i < count($tmp_name_array); $i++){
$fileext [$i] = explode('.', $name_array[$i]);
$fileactualext[$i] = strtolower(end($fileext[$i]));
$allowed = array("jpg","jpeg", "pdf");
if (in_array($fileactualext[$i], $allowed)) {
if(move_uploaded_file($tmp_name_array[$i], "test_uploads/".$name_array[$i])) {
echo $name_array[$i]."upload is complete<br>";
} else {
echo "move_uploaded_file function failed for ".$name_array[$i]."<br>";
}
} else {
header('location:file_type.php');
}
}
}
?>
答案 0 :(得分:0)
使用以下代码检查图像大小,类型,错误,
$file_ext=strtolower(end(explode('.',$_FILES['file_array']['name'])));
$extensions= array("jpeg","jpg","png");
if(in_array($file_ext,$extensions)=== false){
$errors[]="extension not allowed, please choose a JPEG or PNG file.";
}
if($file_size > 2097152){ // set whatever max size you want
$errors[]='File size must be excately 2 MB';
}
if(empty($errors)==true){
move_uploaded_file($file_tmp,"images/".$file_name);
echo "Success";
}else{
print_r($errors);
}