我想使用codeigniter将两个图像文件上传到两个单独的文件夹

时间:2014-01-29 08:52:24

标签: php html codeigniter file-upload

在我的html表单中我有两个文件输入字段

<div style="width: 125px; height: 35px; float: left; font-weight: bold; font-size: 16px;">
                    Image Large
                </div>
                <div style="width: 125px; height: 35px; float: left; margin-top: 15px;">
                    <input  type="file" name="large_image" id="large_image"/>
                </div>
                <div style="clear: both;"></div>

                <div style="width: 125px; height: 35px; float: left; font-weight: bold; font-size: 16px;">
                    Image Small
                </div>
                <div style="width: 125px; height: 35px; float: left; margin-top: 15px;">
                    <input  type="file" name="small_image" id="small_image"/>
                </div>

在我的控制器文件中我有两个上传小图像和大图像的功能。这两个函数由一个函数执行,如

private function insert_products() {
$this->upload_prodct_img_small();
        $this->upload_prodct_img_large();
}

两个上传功能就是这样。

private function upload_prodct_img_large() {
        $image_name = $this->get_image_name_uploade();
        $config['upload_path'] = 'uploaded/large/';
        $config['file_name'] = $image_name;
        $config['overwrite'] = 'FALSE';
        $config['allowed_types'] = 'png|PNG';
        //$config['max_size'] = '1000';
        //$config['max_width'] = '720';
        //$config['max_height'] = '960';
        $config['remove_spaces'] = TRUE;
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('large_image')) {
            $error = array('error' => $this->upload->display_errors());
            $this->session->set_flashdata('error', $error);
        } else {
            $error = 'Successfully Uploaded';
            $this->session->set_flashdata('error', $error);
        }
    }

    private function upload_prodct_img_small() {
        $image_name_small = $this->get_image_name_uploade();
        $config['upload_path'] = 'uploaded/';
        $config['file_name'] = $image_name_small ;
        $config['overwrite'] = 'FALSE';
        $config['allowed_types'] = 'png|PNG';
        //$config['max_size'] = '1000';
        //$config['max_width'] = '180';
        //$config['max_height'] = '240';
        $config['remove_spaces'] = TRUE;
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('small_image')) {
            $error = array('error' => $this->upload->display_errors());
            $this->session->set_flashdata('error', $error);
        } else {
            $error = 'Successfully Uploaded';
            $this->session->set_flashdata('error', $error);
        }
    }

在这两种情况下,我都将文件重命名为$ config数组中的单个文件名。

问题是这个。当我尝试上传两个文件时,只有“small_image”正在上传,而另一个文件总是丢失。 你可以看到我已经评论出宽度的高度和尺寸。因为如果我没有这样做,它会给我一个错误,说“你上传的文件超出了允许的高度或宽度”(类似的东西)。
 我已经仔细检查过我上传的文件大小与允许的文件大小完全相同。

另一种情况是我做了类似的事。

 private function insert_products() {
    //$this->upload_prodct_img_small();
      $this->upload_prodct_img_large();
    }

然后上传大图片而不会出现任何错误。

找不到我做错了什么?

3 个答案:

答案 0 :(得分:2)

在第二个函数中加载上传库之前,只需初始化新的配置数组。

即。通过包含以下行来初始化$ config:

$this->upload->initialize($config);

行之前:

$this->load->library('upload', $config);
upload_prodct_img_small()函数中的

通常,在将新配置与库一起使用之前,您必须初始化新配置,否则将使用默认配置。在您的情况下,默认是您的第一个配置。

答案 1 :(得分:1)

这是我使用的,我测试了它,它对我来说只是阅读评论。

set_time_limit(0);
$config['upload_path'] = './assets/uploads/pictures';
$config['allowed_types'] = 'png';
$config['max_size'] = '1000';
$config['width']     = 188;
$config['height'] = 187;

//load library with first config
$this->load->library('upload', $config);

if ( ! $this->upload->do_upload())
{
    $error = array('error' => $this->upload->display_errors());
    $this->view_data['system_message'] = $this->_msg('n',' '.$error['error']);
}
else
{
    //if data has been uploaded successfully get the newly uploaded file and process
    //this is the data for the newly uploaded file
    $data = array('upload_data' => $this->upload->data());
    //resize start
    $source_path = $data['upload_data']['full_path'];
    //new path i want to put the resized image
    $target_path = './assets/uploads/thumbs';

    //just create thumbs
    $thumb['image_library'] = 'gd2';
    $thumb['source_image']  = $source_path;
    $thumb['new_image'] = $target_path;
    $thumb['create_thumb'] = TRUE;
    $thumb['maintain_ratio'] = FALSE;
    $thumb['width']  = 100;
    $thumb['height'] = 100;
    $this->image_lib->initialize($thumb); 
    $this->image_lib->resize();
    $this->image_lib->clear();

    //resize image
    $thumb2['image_library'] = 'gd2';
    $thumb2['source_image'] = $source_path;
    $thumb2['new_image']    = $data['upload_data']['file_path'];
    $thumb2['create_thumb'] = FALSE;
    $thumb2['maintain_ratio'] = TRUE;
    $thumb2['width'] = 200;
    $thumb2['height'] = 200;
    $this->image_lib->initialize($thumb2);
    $this->image_lib->resize();
    $this->image_lib->clear();
    //resize end

答案 2 :(得分:0)

这里是工作示例... 多于两张图片

公共函数add($ ids1 = null){

   $data['page_title']='ADD';
           if ($this->input->post()) {
   
               $this->form_validation->set_rules('name', 'Name', 'required');
               if ($this->form_validation->run() == FALSE) {
                   
               } else {
                   $cat_file = '';
                   $banner_file = '';
                   if (!empty($_FILES['image']['name'])) {
                       try {
                           $cat_file = $this->fileuploadmain($_FILES);
                       } catch (Exception $e) {
                           log_message('category images', $e->getMessage());
                       }
                   }
                   if (!empty($_FILES['banner_image']['name'])) {
                       try {
                           $banner_file = $this->fileuploadmainbanner($_FILES);
                       } catch (Exception $e) {
                           log_message('banner image category error', $e->getMessage());
                       }
                   }
               }
           }
   
           $this->maintheme('add', $data);
       }
   
   
       public function fileuploadmain($filpost) {
           $path = FCPATH . 'assets/category';
           if (!file_exists($path)) {
               mkdir($path, 0777, true);
           }
           $config['upload_path'] = $path;
           $config['allowed_types'] = 'jpg|png|jpeg|JPG';
           $config['max_size'] = 5000;
           $config['min_width'] = 400;
           $config['min_height'] = 300;
   
           $this->load->library('upload', $config);
           if (!$this->upload->do_upload('image')) {
               $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
               $error = array('error' => $this->upload->display_errors());
               $this->session->set_flashdata('error', $this->upload->display_errors());
               $this->form_validation->set_rules('file', 'File', 'required');
               return false;
           } else {
               $upload_data = $this->upload->data();
               $file_name = $upload_data['file_name'];
               $thumrest = $this->image_thumb($config['upload_path'], $file_name);
               if ($thumrest) {
                   return $file_name;
               } else {
                   return FALSE;
               }
           }
       }
   
       public function fileuploadmainbanner($filpost) {
           $path = FCPATH . 'assets/category/banner';
           if (!file_exists($path)) {
               mkdir($path, 0777, true);
           }
           $config1['upload_path'] = $path;
           $config1['allowed_types'] = 'jpg|png|jpeg|JPG';
           $config1['max_size'] = 5000;
   
           $this->load->library('upload', $config1);
           // here is the most important line
           $this->upload->initialize($config1);
           if (!$this->upload->do_upload('banner_image')) {
               $this->form_validation->set_error_delimiters('<p class="error">', '</p>');
               $error = array('error' => $this->upload->display_errors());
               $this->session->set_flashdata('error', $this->upload->display_errors());
               $this->form_validation->set_rules('banner_image', 'File', 'required');
               return false;
           } else {
               $upload_data1 = $this->upload->data();
               $file_name1 = $upload_data1['file_name'];
               $thumrest1 = $this->image_thumb($config1['upload_path'], $file_name1);
               if ($thumrest1) {
                   return $file_name1;
               } else {
                   return FALSE;
               }
           }
       }
   
       public function image_thumb($folder_name, $image_name) {
           $origin_folder = $folder_name;
           $width = 200;
           $height = 200;
           $folder_name = $folder_name . '/thumbs';
           if (file_exists($folder_name) == false) {
               mkdir($folder_name, 0777);
           }
           // Path to image thumbnail
           $image_thumb = dirname($folder_name . '/' . $image_name) . '/' . $image_name;
           if (!file_exists($image_thumb)) {
               // LOAD LIBRARY
               $this->load->library('image_lib');
               // CONFIGURE IMAGE LIBRARY
               $config['image_library'] = 'gd2';
               $config['source_image'] = $origin_folder . '/' . $image_name;
               $config['new_image'] = $image_thumb;
               $config['maintain_ratio'] = TRUE;
               $config['height'] = $height;
               $config['width'] = $width;
               $this->image_lib->initialize($config);
               $this->image_lib->resize();
               $this->image_lib->clear();
               return true;
           }
       }
   
   }