我想确保上传的图片有尺寸。如果图片尺寸大于50
,我会运行一项不允许上传的测试。这是我的代码:
if(isset($_FILES['fupload'])) {
$filename = strtolower(($_FILES['fupload']['name']));
$ext = substr($filename, -4);
$filename = explode('.',$filename);
$filename = $filename[0];
$filename = str_shuffle($filename);
$filename = $filename.$ext;
$source = $_FILES['fupload']['tmp_name'];
$target = $path_to_image_directory.$filename;
$size = getimagesize($filename);
if( $size[0] > 20 || $size[1] > 20 ) {
echo 'Too Large!';
} else {
move_uploaded_file($source, $target);
createThumbnail($filename);
$title= mysqli_real_escape_string($connection,$_POST['title']);
$keyword1 = mysqli_real_escape_string($connection,$_POST['keyword1']);
$keyword2 = mysqli_real_escape_string($connection,$_POST['keyword2']);
$keyword3 = mysqli_real_escape_string($connection,$_POST['keyword3']);
$Q = "INSERT INTO images (name, title, keyword1,keyword2, keyword3)VALUES('$filename', '$title', '$keyword1','$keyword2','$keyword3')";
if(!mysqli_query($connection, $Q)) {
die("error inserting into pres name".mysqli_error($connection));
}
}
}
我希望这条消息“太大了!”#34;当我尝试上传大图片时,我不知道。图片上传无论有多大。我的最终目标是将大于号转换为小于符号,以确保文件具有实际尺寸。
我从stackoverflow得到了这个条件语句:
if($size[0] > 20 || $size[1] > 20) {
echo 'Too Large!';
}
它似乎应该阻止上传,但它没有。有人可以帮我理解吗?
答案 0 :(得分:0)
你可以使用
list($width, $height, $type, $attr) = getimagesize($_FILES["fupload"]["tmp_name"]);
if(($width>20)||($height>20)){
$error = "Image Dimemsions Exceed Allowed Dimensions";
return $error;
}
希望它有所帮助;)