在CodeIgniter中上传和调整大小

时间:2015-03-03 11:21:03

标签: php codeigniter-routing

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

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);

                }
            }
        }

我错过了什么,重新调整大小的功能是不正常的。

2 个答案:

答案 0 :(得分:0)

在这一行之下

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

echo $path;

然后检查输出是什么?

答案 1 :(得分:0)

请尝试以下

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]);


        }
     }

}    

}
 }