重命名文件后上传Codeigniter如何将新文件名保存到我的数据库

时间:2014-05-23 19:37:33

标签: php codeigniter

我有codeigniter文件上传库的问题。我正在尝试上传多个文件(7张图片),我重命名它们,然后上传到我的服务器文件系统。但我无法将新名称保存在我的数据库中。

这是我的控制器

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 ...

2 个答案:

答案 0 :(得分:0)

您在Codeigniter文件上传类中有一个帮助函数:$this->upload->data()。为了得到文件名,请执行以下操作:

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

查看手册here

编辑: 将数据插入到数据库中,假设$ filename是一个字符串,您需要先将其分解为数组

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

然后您可以将数据(imagename)插入到表img的{​​{1}}列中,如下所示:

test

答案 1 :(得分:0)

可用于

前:

$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'],
    );