图片上传,大小写在JPG扩展名不起作用 [英] Image uploader, capital letters in JPG extension doesn't work

查看:1298
本文介绍了图片上传,大小写在JPG扩展名不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作图片上传器,但由于某种原因,我不允许以大写字母上传JPG图片。这怎么可能?



我也尝试将JPG添加到allowedExts数组中,但这也不起作用。

  $ filesize ='8500'; //将文件大小设置为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] ==图片/ jpeg)
||($ _FILES [file] [type] ==image / png)
||($ _FILES [file] [type ]
&&($ _FILES [file] [size]< $ filesize)
&& in_array($ extension,$ ){
if($ _FILES [file] [error]> 0){
echo返回代码:。 $ _FILES [file] [error]。 <峰; br> 中;

其他{
if(file_exists(source / images /。$ _FILES [file] [name])){
echo'image already exists ;

$ {b $ b //将原始文件上传到文件夹
}
}
}
else {
echo'Wrong文件格式';

输出结果如下:



字符串''(长度= 0)
字符串'JPG'(长度= 3)
$ b

错误的文件格式

解决方案

PHP字符串比较区分大小写:

 &安培;&安培; in_array($ extension,$ allowedExts)){

如果您上传 kitten.JPG ,因为在你允许的扩展数组中, .JPG NOT .jpg 是,但就PHP而言,这是完全不同的字符串。你应该使用strtolower来标准化你从上传的文件名获得的扩展名,或者至少使用一个不区分大小写的比较,比如 strcasecmp < a>



注意你的文件处理逻辑不正确。你显然已经抓住了一个非常广泛分布的坏例子。您需要检查上传的 VERY 首先是 ['error'] 参数。如果这不是零,那么你不能相信$ _FILES数组中的其他任何特定文件。不要检查大小,不要检查MIME类型,不要检查文件名。如果上传失败,这些都可能是不存在/不正确的/等等...


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"] . "<br>";
    }
    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...

这篇关于图片上传,大小写在JPG扩展名不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆