通过codeigniter多重上传图片并创建拇指

时间:2012-10-14 13:33:55

标签: php codeigniter

我有问题,我带了2个小时,但我无法解决。

当我想上传多张图片(3张图片)时,它会上传,但之后我无法为所有人创建一个拇指,仅针对第一张图片,我不知道问题出在哪里

这是我的表格

<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="hotel_img1" value="upload" name="hotel_img1">
<input type="file" id="hotel_img2" value="upload" name="hotel_img1">
<input type="file" id="hotel_img3" value="upload" name="hotel_img1">
<input type="submit">
</form>

这是我的代码 控制器

<?php
if(!empty($_FILES))
    $upload_image = array('1','2','3','4','5');
    foreach($upload_image as $i) {
    if(!empty($_FILES["hotel_img$i"]['name']))
    {
        $hotel_ID= 12;
        $config['file_name'] = $this->session->userdata('user_id').'-'.$hotel_ID.'-'.time().$i;
        $config['upload_path'] =realpath(APPPATH . '../img');
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '512';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';
        $config['overwrite'] = TRUE;
        $this->upload->initialize($config);
        $form_name = 'hotel_img' . $i;
        if(!$this->upload->do_upload($form_name))
            {       
                $data['msg'] .= "ERROR";
            }
            else
            {   
                $file_data = $this->upload->data();
                $image_data['img_url'] = $file_data['file_name'];
                $image_data['img_size'] = $file_data['file_size'] * 1024;
                $image_data['img_forID'] = $hotel_ID;
                $this->model_hotels->insert_image($image_data);
                $data['msg'] .= "Uploaded Picture No $i";
                $config_thumb['image_library'] = 'gd2';
                $config_thumb['source_image'] = $file_data['full_path'];
                $config_thumb['maintain_ratio'] = FALSE;
                $config_thumb['width'] = 250;
                $config_thumb['height'] = 150;
                $config_thumb['new_image'] = realpath(APPPATH . '../img').'/thumb/thumb_' .  $file_data['file_name'];
                $this->load->library('image_lib', $config_thumb);
                $this->image_lib->resize();
                $this->image_lib->clear(); 
            }
        }
    }
    $this->model_hotels->set_hotel_thumb($hotel_ID);
}   ?>

2 个答案:

答案 0 :(得分:0)

对我来说,$ file_data始终是相同的,并且可能包含一个包含其他图片的数组。现在你的代码说每张图片调整大小$ file_data,它只选择上传的第一张图片。尝试print_r($ file_data),因为我猜这就是造成问题的原因。

答案 1 :(得分:0)

尝试将此添加到您的配置中:

$config['create_thumb']