Codeigniter图片上传只会上传到一个目录

Kev*_*own 2 php upload codeigniter

function upload($path){
  $config['overwrite'] = TRUE;
  $config['allowed_types'] = 'jpg|jpeg|gif|png';
  $config['max_size'] = 2000;

  if($path =='profile'){
   $config['upload_path'] = '../assets/uploads/avatars';
   $config['file_name'] = $this->id;  
  }
  if($path =='company'){
   $config['upload_path'] = '../assets/uploads/company';
   $config['file_name'] = $this->id.'_company';
  }

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

  $image_data = $this->upload->data();
  if($path == 'profile'){
   $this->db->update('be_user_profiles',array('avatar' => $image_data['file_name']), array('user_id' => $this->id));
  }
  if($path == 'company'){
   $this->db->update('be_user_profiles',array('company_logo' => $image_data['file_name']), array('user_id' => $this->id));
  }
  $config = array(
   'source_image' => $image_data['full_path'],
   'new_image' => $this->upload_path,
   'maintain_ratio' => true,
   'width' => 500,
   'height' => 500
  );

  $this->load->library('image_lib', $config);
  $this->image_lib->resize();
 }
Run Code Online (Sandbox Code Playgroud)

这是我的上传功能,它可以正常工作$path = 'profile',但是当它company,它不会上传到"公司"文件夹...有什么理由应该这样吗?!我在这里不知所措...这个功能在它进入头像文件夹时有效,但如果它进入公司文件夹却没有...

小智 9

我有多个文件夹的类似问题.我修复的方法是使用初始化函数,而不是将配置作为参数传递给加载库函数.

$this->upload->initialize($config);
Run Code Online (Sandbox Code Playgroud)

您可以加载库然后设置您的配置并调用initialize方法.

希望这可以帮助.