想在数据库中存储多个文件路径

时间:2014-11-18 06:26:24

标签: php mysql codeigniter upload

您好我想在数据库中存储多个文件路径,但以下代码只存储一个文件路径

请帮助。

我的控制器

<?php    
class upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    function index()
    {
        $this->load->view("header.php");
        $this->load->view('main_views', array('error' => ' ' ));
        $this->load->view("footer.php");
    }

function do_upload()
{
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '2000';
    $this->load->library('upload', $config);


    if ( ! $this->upload->do_upload())
    {
        // no file uploaded or failed upload
        $error = array('error' => $this->upload->display_errors());
        $this->load->view("header.php");
        $this->load->view('main_views', $error);
        $this->load->view("footer.php");
    }
    else
    {
       $data = array('upload_data' => $this->upload->data());

        $this->load->model('post_model');
        $this->post_model->main_model();

        $this->load->view("header.php");

        $this->load->view('main_views');
        $this->load->view("footer.php");

    }
}
?>

我的模特

<?php
class post_model extends CI_Model
{

    function main_model()
    { 
        $filepath = $this->upload->data()['file_name']; 
        $this->db->trans_begin();

        $this->db->query("insert into post (post_pic)
         values ('$filepath')");


        $ad_id = $this->db->insert_id();

    }

}
?>

视图

                <span id="filediv"><input name="userfile" type="file" id="file"/>
       </span>
                <span id="filediv"><input name="userfile2" type="file" id="file"/>
       </span>
                <span id="filediv"><input name="userfile3" type="file" id="file"/>
       </span>
                <span id="filediv"><input name="userfile2" type="file" id="file"/></span><br/>

由于解析规则而产生的额外文本因为解析规则而忽略了额外的文本因为解析规则而忽略了额外的文本因为解析规则而忽略了额外的文本因为解析规则而忽略了额外的文本请善意忽略它

3 个答案:

答案 0 :(得分:1)

浏览 - 输入名称更改为 name =“userfile []”

答案 1 :(得分:0)

您必须上传每个$ _FILES

foreach ( $_FILES as $key => $value ) {

    if (! empty ( $value ['name'] )) {

        //change the file name
        $config ['file_name']       = date('Y_m_d') . '_' . $value ['name'];    
        $this->upload->initialize($config);

        if (! $this->upload->do_upload ( $key )) {

            // handle error function
            $this->handle_error ( $this->upload->display_errors () );

        } else {

            $files [$i] = $this->upload->data ();
            ++ $i;
        }
    }
}

在$ files数组中,您将获得上传文件的详细信息,这些文件可以进一步用于模型以将数据路径插入数据库。

答案 2 :(得分:0)

您可以使用多次上传codeigniter

https://github.com/stvnthomas/CodeIgniter-Multi-Upload

由于

相关问题