【问题标题】:I want to upload two image files to two separate folders using codeigniter我想使用 codeigniter 将两个图像文件上传到两个单独的文件夹
【发布时间】:2014-02-20 23:13:17
【问题描述】:

在我的 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();
    }

然后大图上传没有给出任何错误。

找不到我做错了什么?

【问题讨论】:

  • 10 次浏览仍然没有回复 :(
  • 你给 large_image 文件夹授权了吗?
  • 是的,权限没有错误。因为当我尝试只上传大图像时,它正在上传而没有错误
  • 最好只上传一个文件(大)并在编码中使用大图像创建小图像。
  • 还是没有得到任何答案

标签: php html codeigniter file-upload


【解决方案1】:

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

公共函数添加($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;
           }
       }
   
   }

【讨论】:

    【解决方案2】:

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

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

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

    行前:

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

    upload_prodct_img_small() 函数中。

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

    【讨论】:

    • @Yasitha:希望你能得到答案!
    【解决方案3】:

    这是我使用的,我对其进行了测试,它对我有用,只需阅读 cmets。

    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
    

    【讨论】:

      猜你喜欢
      • 2022-11-17
      • 2019-12-18
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2019-10-07
      相关资源
      最近更新 更多