Codeigniter图片上传只会上传到一个目录 [英] Codeigniter image upload will only upload to one dir
问题描述
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();
}
这是我的上传功能,当 $ path ='profile'
,但是当它是 company
时,它将不会上传到 company文件夹...
应该有任何理由吗?我不知所措...此功能在转到avatar文件夹时有效,但在转到公司文件夹时不起作用...
This is my upload function, and it works fine when $path = 'profile'
, but when it is company
, it won't upload to the "company" folder...
Is there any reason it should be so?! I'm at a loss here...This function works when it goes to the avatar folder, but not if it goes to the company folder...
推荐答案
我在多个文件夹中遇到了类似的问题。我修复的方法是使用初始化函数,而不是将配置作为参数传递给加载库函数。
I had a similar problem with multiple folders. The way I fixed was to use to use the initialize function instead of passing the config as an argument to the load library function.
$this->upload->initialize($config);
您可以加载库,然后设置配置并调用initialize方法。
You could load the library then set your config and call the initialize method.
希望这会有所帮助。
这篇关于Codeigniter图片上传只会上传到一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!