CI 文件上传,错误显示 if/else 会导致图像重复



我以某种方式设法上传和调整了多个图像的大小,一切都很好。
然后我想在没有选择要上传的图像时显示错误。
我放了一个 if 语句来加载错误,但这会造成严重破坏。

每次我选择图像并上传时,最后一个都会重复,而不会调整大小。
同样,如果我删除ifelse,那就完全没问题了。

感谢您的任何帮助和您的时间。

function do_upload(){

    $path = array();
    $count = count($_FILES['userfile']['size']);
    foreach($_FILES as $key=>$value){
        for($n=0; $n<=$count-1; $n++) {
            $_FILES['userfile']['name']=$value['name'][$n];
            $_FILES['userfile']['type']    = $value['type'][$n];
            $_FILES['userfile']['tmp_name'] = $value['tmp_name'][$n];
            $_FILES['userfile']['error']       = $value['error'][$n];
            $_FILES['userfile']['size']    = $value['size'][$n];   
                $config['upload_path'] = './images';
                $config['allowed_types'] = 'gif|jpg|png|jpeg';
            $this->load->library('upload', $config);
            $this->upload->do_upload();
            $data = $this->upload->data();
            $path[] = $data['full_path'];
        }   

        if(!$this->upload->do_upload()){
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('view_dashboard_error_car', $error);
        }
        else{
            $this->load->library('image_lib');   
            foreach($path as $p=>$ath){
                $config1 = array(
                'source_image'      => $ath,
                'new_image'         => './images',
                'maintain_ration'   => true,
                'overwrite'         => true, 
                'width'             => 600,
                'height'            => 400
                );
                $this->image_lib->initialize($config1);
                $this->image_lib->resize();
                $this->image_lib->clear();
            }       

            $this->load->view('view_dashboard_success_car');
        }


    }   


}

您运行了两次$this->upload->do_upload(),因为您的foreach()运行,然后运行if/else语句。

您需要将if/else放入foreach循环中。您应该注意不要只是复制/粘贴它,而是要确保每个图像只运行一次$this->upload->do_upload()

最新更新