doupaimo8288 2012-04-10 11:36
浏览 33
已采纳

借助数组上传图像

yesterday i started coding an upload file for uploading images to a directory. as i recognized that i have to use arrays to handle that a better way i got to the point that i'm using now:

 if(!empty($_FILES['image']['tmp_name'])){  

                $allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');

                foreach($_FILES['image']['name'] as $key => $array_value){

                    $file_name = $_FILES['image']['name'][$key];
                    $file_size = $_FILES['image']['size'][$key];
            $file_tmp = $_FILES['image']['tmp_name'][$key];

                    $file_extension = strtolower(end(explode('.', $file_name)));
                    if (in_array($file_extension, $allowed_extension) === false){
                        $errors[] = 'file is not accepted';
                        }

                    if ($file_size > 2097152){
                        $errors[] = 'maxsize: 2MB';
                        }

                    $path = "a/b/c/"; 
                    $uploadfile = $path."/".basename($_FILES['image']['name'][$key]);

                    if (move_uploaded_file($_FILES['image']['tmp_name'][$key], $uploadfile)){
                        echo "Das Bildmaterial wurde hochgeladen.";
                        }
                } 
        }

the problem is that i can upload the files all the time even when a non accepted ending is given. i dont understand why the upload happens when instead of uploading the errormessage should have been showed. i like to reach some more security with embedding allowed extensions to the code. if there is someone who could tell me what am i doing wrong, i would really appreciate. thanks a lot.

  • 写回答

2条回答 默认 最新

  • dqybjj3497 2012-04-10 11:43
    关注

    You have two if-statements checking for exceptions but then do nothing with them.

    Perhaps the following will help?

    if(!empty($_FILES['image']['tmp_name'])){  
    
        $allowed_extension = array('jpg', 'jpeg', 'png', 'bmp', 'tiff', 'gif');
    
        foreach($_FILES['image']['name'] as $key => $array_value){
    
            $file_name = $_FILES['image']['name'][$key];
            $file_size = $_FILES['image']['size'][$key];
            $file_tmp = $_FILES['image']['tmp_name'][$key];
    
            $errors = array ();
            $file_extension = strtolower(end(explode('.', $file_name)));
            if (in_array($file_extension, $allowed_extension) === false){
                $errors[] = 'file is not accepted';
            }
    
            if ($file_size > 2097152){
                $errors[] = 'maxsize: 2MB';
            }
    
            if (count ($errors) == 0) {
                $path = "a/b/c/"; 
                $uploadfile = $path."/".basename($_FILES['image']['name'][$key]);
    
                if (move_uploaded_file($_FILES['image']['tmp_name'][$key], $uploadfile)){
                    echo "Das Bildmaterial wurde hochgeladen.";
                }
            }
            else {
                // do stuff with errors
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器