PHP图像上传检查尺寸 [英] PHP Image Upload Checking Dimensions
本文介绍了PHP图像上传检查尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在上传图片后查看图片的尺寸,如果图片与我想要的尺寸不符合,则
将其删除?
How can I check the dimensions of an image after it has uploaded and delete it if it does not match the dimensions I want?
所以在挖掘之后我发现PHP无法做尺寸。我所遵循的解决方案是:
So after digging around I find PHP cannot do dimensions. The solution I am following is:
- 将文件上传到服务器
- 使用新字符串并检查
- 如果
与宽度和高度不匹配,请将其删除或继续上传
这是我的代码。有人可以告诉我如何检查当前文件的尺寸以及如果不匹配如何删除文件夹和文件?
This is my code. Can someone show me how to check the current file for dimensions and how to delete the folder and file if not matching?
# create our temp dir
mkdir("./uploads/temp/".$user."/".$mx."/".$hash."/", 0777, true);
# upload dir settup
$uploaddir='./uploads/temp/'.$user.'/'.$mx.'/'.$hash.'/';
$file=$uploaddir . basename($_FILES['file']['name']);
# upload the file first
if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) {
# ok so check the height and width
# if it is not the width and height assigned delete image and folder
if (image height= and width =){
unlink($file);
rmdir($uploaddir);
$result=0;
} else {
# image matches height and width message ok
$result=1;
}
} else {
# error uploading
$result=$errormsg;
}
推荐答案
mkdir("./uploads/temp/".$user."/".$mx."/".$hash."/", 0777, true);
# upload dir settup
$uploaddir='./uploads/temp/'.$user.'/'.$mx.'/'.$hash.'/';
$file=$uploaddir . basename($_FILES['file']['name']);
# upload the file first
if (move_uploaded_file($_FILES['file']['tmp_name'], $file)) {
# ok so check the height and width
# if it is not the width and height assigned delete image and folder
$size = getimagesize($files);
$maxWidth = 500;
$maxHeight = 500;
if ($size[0] > $maxWidth || $size[1] > $maxHeight)
{
unlink($file);
rmdir("./uploads/temp/".$user."/".$mx."/".$hash."/");
rmdir("./uploads/temp/".$user."/".$mx."/");
rmdir("./uploads/temp/".$user."/");
}
else
$result=1;
end if
} else {
# error uploading
$result=$errormsg;
}
这篇关于PHP图像上传检查尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文