2016-07-05 63 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); 

會與實際文件的文件擴展名指定新的名字

相關問題