2016-07-05 64 views
0

我使用PHP Codeigniter处理项目。我想上传图片,更改图片的名称并将其存储在数据库中(带扩展名)。我写了下面的代码。它完美的工作,但问题是,图像名称存储没有在数据库中的扩展名。例如,当我运行代码时,存储在数据库中的名称是'imagename',而不是'imagename.jpg'或'imagename.png'。我想存储带有扩展名的完整名称。更改图像名称并将其与数据库存储在一起php codeigniter

这是我的控制器

public function FunctionName() 
{ 
    $fname = ucwords($this->input->post('fname')); 
    $lname = ucwords($this->input->post('lname')); 
    $prof_pic = $_FILES['profile_pic']['name']; 

    $config = array ('upload_path' => './images/students/', 
     'allowed_types' => "jpeg|jpg|png", 
     'overwrite' => TRUE, 
     'file_name' => $fname."_".$lname, 
     'remove_spaces' => TRUE, 
     'file_ext_tolower' => TRUE 
    ); 

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

    $image_name = $config['file_name'].".".$config['file_type']; 

    $data = array('std_fname' => $std_fname, 
     'std_lname' => $std_lname, 
     'profile_pic' => $image 
    ); 
    $this->General_Model->create_record($data, "table_name"); 
} 
+0

我没有看到'在file_type'定义了'$ config'。 – chris85

回答

0

如要上传的文件名使用

$this->upload->data('file_name'); 

AS

$this->upload->do_upload('profile_pic'); 
$image_name = $this->upload->data('file_name'); 

Information about the uploaded file

0

尝试用下面的代码

$prof_pic = $_FILES['profile_pic']['name']; 
$newName = "<Desired name>".".".pathinfo($prof_pic, PATHINFO_EXTENSION); 
$config = array ('upload_path' => './images/students/', 
    'allowed_types' => "jpeg|jpg|png", 
    'overwrite' => TRUE, 
    'file_name' => $newName, 
    'remove_spaces' => TRUE, 
    'file_ext_tolower' => TRUE 
); 

$newName = "<Desired name>".".".pathinfo($prof_pic, PATHINFO_EXTENSION); 

会与实际文件的文件扩展名指定新的名字

相关问题