CodeIgniter中的上载和调整大小


Upload and Resize in CodeIgniter

我正在上传并重新调整多个图像的大小,但这段代码适用于1个文件,如果我上传2个或更多文件,它会将第一张图片的大小重新调整为完全黑色,并且上传的所有其他图片不变。

function do_upload()
        {
            $files = $_FILES;
            $cpt = count($_FILES['userfile']['name']);
            for($i=0; $i<$cpt; $i++)
            {
                $_FILES['userfile']['name']= $files['userfile']['name'][$i];
                $_FILES['userfile']['type']= $files['userfile']['type'][$i];
                $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
                $_FILES['userfile']['error']= $files['userfile']['error'][$i];
                $_FILES['userfile']['size']= $files['userfile']['size'][$i];
                $config['upload_path'] = './uploads/';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size'] = '100';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $this->load->library('upload', $config);
                if ( ! $this->upload->do_upload())
                {
                    $error = array('error' => $this->upload->display_errors());
                    $this->load->view('upload_form', $error);
                }
                else
                {
                    $data = array('upload_data' => $this->upload->data());
                    $path=$data['upload_data']['full_path'];
                    $q['name']=$data['upload_data']['file_name'];
                     $configi['image_library'] = 'gd2';
                     $configi['source_image']   = $path;
                     $configi['maintain_ratio'] = TRUE;
                     $configi['width']  = 75;
                     $configi['height'] = 50;
                     $this->load->library('image_lib', $configi);
                     $this->image_lib->resize();
                    $this -> load -> view('upload_success', $q);
                }
            }
        }

在我遗漏的地方,重新调整大小的功能不准确。

这条线下的

$path=$data['upload_data']['full_path'];

写入

echo $path;

然后检查输出是什么?

请尝试以下

function do_upload(){
$this->load->library('upload');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']  = '1024';
$config['max_height']  = '768';
 foreach($_FILES['userfile'] as $key => $value)
{
    if( ! empty($key['name']))
    {
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload($key))
        {
            $error['error'] = $this->upload->display_errors();
            $this->load->view('pages/uploadform', $error);
        }    
        else
        {
            $data[$key] = array('upload_data' => $this->upload->data());
            $this->load->view('pages/uploadsuccess', $data[$key]);

        }
     }
}    

}
}