java图片后缀大小写,图片上传,大小写在JPG扩展名不起作用

I'm making an image uploader but for some reason i'm not allowed to upload JPG images in capital letters. How is this possible?

I also tried to add JPG to the allowedExts array but that is also not working.

$filesize = '8500'; // PUT the filesize here in KB

if(isset($_FILES["file"])){

$allowedExts = array("jpg", "jpeg", "gif", "png");

$extension = end(explode(".", $_FILES["file"]["name"]));

var_dump($_FILES['file']['type']);

var_dump($extension);

if ((($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/png")

|| ($_FILES["file"]["type"] == "image/pjpeg"))

&& ($_FILES["file"]["size"] < $filesize)

&& in_array($extension, $allowedExts)){

if ($_FILES["file"]["error"] > 0){

echo "Return Code: " . $_FILES["file"]["error"] . "
";

}

else{

if (file_exists("source/images/" . $_FILES["file"]["name"])){

echo 'image already exists';

}

else{

//Upload original file to folder

}

}

}

else{

echo 'Wrong fileformat';

}

As output I get this:

string '' (length=0)

string 'JPG' (length=3)

Wrong fileformat

解决方案

PHP string comparisons are case sensitive:

&& in_array($extension, $allowedExts)){

is going to blow up if you upload kitten.JPG, because .JPG is NOT in your allowed extensions array. .jpg is, but that's a completely different string as far as PHP is concerned. You should normalize the extension you get from the uploaded filename with strtolower, or at least use a case-insentive comparison, such as strcasecmp

And note that your file handling logic is incorrect. You've obviously grabbed a very widely distributed BAD example. The VERY first thing you need to check upon upload is the ['error'] parameter. If that's nonzero, then you cannot trust anything else in the $_FILES array for that particular file. Don't check size, don't check mime types, don't check filenames. If an upload fails, those could all be non-existent/incorrect/etc...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值