更改图像名称并使用php codeigniter将其存储在数据库中

时间:2016-07-05 13:09:22

标签: php codeigniter file-upload

我正在使用PHP Codeigniter开展项目。我想上传图片,更改图片名称并将其存储在数据库中(带扩展名)。我写了以下代码。它工作得很好,但问题是图像名称在数据库中没有扩展名的情况下存储。例如,当我运行代码时,存储在数据库中的名称是“imagename”,而不是“imagename.jpg”或“imagename.png”。我想用扩展名存储完整的名称。

这是我的控制器

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");
}

2 个答案:

答案 0 :(得分:0)

要使用

获取上传的文件名
var herbrewStrFromServer = "⌘" //Herbew String
let dataSaveCoreData = NSKeyedArchiver.archivedDataWithRootObject(herbrewStrFromServer) //Converting into Nsdata so as to save in coredata
let actualStringRetreived = NSKeyedUnarchiver.unarchiveObjectWithData(dataSaveCoreData) as! String // retrieving it back from core data

AS

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

阅读 Information about the uploaded file

答案 1 :(得分: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);

将为新名称指定实际文件的文件扩展名