简体   繁体   English

重命名文件以上传后,Codeigniter如何将新文件名保存到数据库中

[英]Codeigniter after renaming file for upload how do i save the new file name to my database

I have an issue with codeigniter file upload library. 我的codeigniter文件上传库有问题。 I am trying to upload multiple files (7 images), i renamed them and after which uploaded to my server file system. 我正在尝试上传多个文件(7个图像),我将其重命名,然后上传到我的服务器文件系统。 But i cannot get the new name to save in my database. 但是我无法获得要保存在数据库中的新名称。

Here is my Controller 这是我的控制器

if (isset($_POST['carform']))
{
                $dir = './uploads/cars';
                $config['upload_path'] = $dir;
                $config['allowed_types'] = 'jpg|jpeg|png';
                $config['max_size'] = '2048';
                $config['max_width']  = '1024';
                $config['max_height']  = '768';
                $config['remove_spaces'] = 'TRUE';
                $uid = $this->tank_auth->get_user_id();
                $config['file_name'] = time().$uid;
                $this->load->library('upload', $config);

            foreach($_FILES as $field => $file)
            {

                if($file['error'] == 0)
                {

                    // So lets upload
                    if ($this->upload->do_upload($field))
                    {


                        $data = array('upload' => $this->upload->data());

                        $this->cars_model->set_car($data);//end foreach loop.....
                        $id = $this->db->insert_id();
                redirect('/cars/details/'.$id.'');

                    }else
                    {
                        $errors = $this->upload->display_errors();
                        echo($errors);
                    }
                }

            }


        }//end if  statement ...

You do have a helper function within Codeigniter File Uploading Class: $this->upload->data() . 您在$this->upload->data()类中确实有一个辅助函数: $this->upload->data() In order to get the filename do this: 为了获取文件名,请执行以下操作:

$my_data=$this->upload->data();
$file_name=$my_data['filename'];

check the manual here 这里查看手册

edit: to insert data into database assuming $filename is a string you need first explode it into an array 编辑:假设$ filename是字符串,则要将数据插入数据库,您首先需要将其分解为数组

$filename="14010989464.jpg 140109894641.jpg 140109894642.jpg 140109894643.jpg 140109894644.jpg 140109894645.jpg 140109894646.jpg";
$fn=explode(' ',$filename);

then you can insert the data (imagename) into the column img of your table test like this: 然后可以将数据(图像名称)插入表test img列中,如下所示:

foreach ($fn as $key=>$val){
    $data['img']=$fn[$key];
    $this->db->insert('test', $data);
}

can use for 可以用于

ex : 例如:

$this->load->library('upload', $config);
for($i=1;$i<=5;$i++) //5 = total image
{
if (!$this->upload->do_upload('userfile'.$i)) 
{
    $data['error'] = array('error' => $this->upload->display_errors());

}
else
{
    $upload_data = $this->upload->data();
    $data[$i] = $upload_data['file_name'];
}
}
    $data_x = array(

                'image1' =>  $data['1'],
                'image2' => $data['2'],
                'image3' => $data['3'],
                'image4' => $data['4'],
                'image5' => $data['5'],
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM