在codeigniter多个上传配置时遇到问题

时间:2014-04-25 13:57:02

标签: file-upload codeigniter-2

我的上传有3种类型的配置,就是当我上传文件时,它使用相同的配置,如图像路径,所有这些配置并且它们都会被控制。 这是我的代码:

$config['upload_path'] = './media/content/';
                $config['allowed_types'] = 'jpg|jpeg|gif|png';
                $config['max_size'] = '100000';
                $config['max_width']  = '10024';
                $config['max_height']  = '7680';
                $config['remove_spaces']  = TRUE;
                $this->load->library('upload', $config);
                $this->upload->do_upload("Image");
                $ImageData = $this->upload->data();
                $Image=$ImageData['file_name'];
                $ImageFileError=$this->upload->display_errors();
                //--------------------------
                $config2['image_library'] = 'gd2';
                $config2['source_image']    = $_SERVER["DOCUMENT_ROOT"].'/media/content/'.$Image;
                $config2['create_thumb'] = TRUE;
                $config3['maintain_ratio'] = TRUE;
                $config2['width']= 300;
                $config2['height']= 170;
                $config2['new_image']= $_SERVER["DOCUMENT_ROOT"].'/media/content/'.'big_thumb_'.$Image;
                $this->image_lib->initialize($config2);
                $this->load->library('image_lib', $config2); 
                $this->image_lib->resize();
                //---------------
                $config3['image_library'] = 'gd2';
                $config3['source_image']    = $_SERVER["DOCUMENT_ROOT"].'/media/content/'.$Image;
                $config3['create_thumb'] = TRUE;
                $config3['maintain_ratio'] = TRUE;
                $config3['width']= 268;
                $config3['height']= 120;
                $config3['new_image']= $_SERVER["DOCUMENT_ROOT"].'/media/content/'.'small_thumb_'.$Image;
                $this->image_lib->initialize($config3);
                $this->load->library('image_lib', $config3); 
                $this->image_lib->resize();

if($_FILES['ImageHeader']['size'])
                {
                    $config8['upload_path'] = './media/content/headers/';
                    $config8['allowed_types'] = 'jpg|jpeg|gif|png';
                    $config8['max_size']    = '100000';
                    $config8['max_width']  = '10024';
                    $config8['max_height']  = '7680';
                    $config8['remove_spaces']  = TRUE;
                    //$this->image_lib->initialize($config8);
                    $this->load->library('upload', $config8);
                    $this->upload->do_upload("ImageHeader");
                    $ImageDataHeader = $this->upload->data();
                    $ImageHeader=$ImageDataHeader['file_name'];
                    $ImageFileErrorHeader=$this->upload->display_errors();
                    $config11['image_library'] = 'gd2';
                    $config11['source_image']   = $_SERVER["DOCUMENT_ROOT"].'/media/content/headers/'.$ImageHeader;
                    $config11['create_thumb'] = false;
                    $config11['maintain_ratio'] = false;
                    $config11['width']= 960;
                    $config11['height']= 240;
                    //$config3['new_image']= $_SERVER["DOCUMENT_ROOT"].'/media/region/'.'small_thumb_'.$ImageHeader;
                    $this->image_lib->initialize($config11);
                    $this->load->library('image_lib', $config11);
                    $this->image_lib->resize();
                }

我不知道为什么?!!,我已经使用了初始配置来使用新配置,但仍然无法正常工作并将其全部上传到同一个文件夹中。

1 个答案:

答案 0 :(得分:0)

您只为主$ config设置$config['upload_path'] = './media/content/';一次,而对其他配置再也不设置$config2['new_image']= $_SERVER["DOCUMENT_ROOT"].'/media/content/'.'big_thumb_'.$Image;。您还为每个配置使用unset($config); // Unloads the previous configuration $this->image_lib->clear(); // Clears the image_lib for the next configuration to be loaded ,因此您创建的所有文件都将与您声明的文件夹在同一文件夹中。

在加载每个新配置之前,您还应该执行以下操作:

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

然后你可以加载你的下一个配置: {{1}}