上传图像时不希望用空格保存实际图像名称

时间:2015-11-06 09:31:56

标签: codeigniter

在codeigniter中我上传图像并将图像名称存储在数据库中如果实际图像名称如“abc data.jpg”,我想存储图像名称,如abc_data.jpg,并且还有此名称图像应移动到uploads文件夹中

这是我的图片上传代码: -

$config['upload_path']          = './uploads/';
                $config['allowed_types']        = 'gif|jpg|png|jpeg';
                $config['max_size']             = 5000;
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if ($this->upload->do_upload('cover_image')) 
                {
                    $data =$this->upload->data();
                    $data_ary = array(
                                'project_id' => (int)$iProjectId,
                                'image_url' => $data['file_name'],
                                'is_covred_photo' => 'YES'
                        );
                $this->db_project->insert( $this->sTable6 , $data_ary);
                $aResp = array( 'project_images_id' => $this->db->insert_id());
                }

2 个答案:

答案 0 :(得分:1)

添加$config['file_name']

$config['upload_path']          = './uploads/';
$config['allowed_types']        = 'gif|jpg|png|jpeg';
$config['max_size']             = 5000;
$config['file_name']            = 'My_new_file_name'; # Add here

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

也删除此行

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

答案 1 :(得分:0)

试试这个:

$file_name = $_FILES['file_view_name']['name'];
$file_name_pieces = split('_', $file_name);
$new_file_name = '';
$count = 1;

foreach($file_name_pieces as $piece)
{
    if ($count !== 1)
    {
        $piece = ucfirst($piece);
    }

    $new_file_name .= $piece;
    $count++;
}

$config['file_name'] = $new_file_name;

Refer this