文件上传&使用CodeIgniter的文件上传类进行数据输入

时间:2012-09-19 10:48:15

标签: php codeigniter

我正在开发一个允许用户上传图像和数据的表单。 我之前已经构建了表单,但我正在尝试将文件上传类与我以前的控制器集成。

以下是Controller,Model&从我的视图形式,任何人都可以帮助我绑定缺失的链接?我已经搜遍了所有&没找到解决方案

class Canvas extends CI_Controller {

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

}

function index() {
            $vars = array();
            $this->load->library('FacebookConnect');
            $facebook=$this->facebookconnect->connect();
            $vars['facebook'] = $facebook;
            $user = $vars['user'] = $facebook->getUser();
            $this->load->model('Users_Model'); 
            if($user != 0  && sizeof($_POST)>0) {
                // user already set up. Show thank you page
                $this->iterate_profile ($vars['user'],false,$_POST);
                $this->load->view('upload_form',$vars);
            } else {
                // user not set, show welcome message
                $this->load->view('canvas',$vars);
            }

}
    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            $this->load->view('upload_form', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());

            $this->load->view('upload_success', $data);
        }
    }
    function thank_you() {
            $this->load->view('thank_you',$vars);
    }

    function remove() {
            $vars = array();
            $this->load->library('FacebookConnect');
            $facebook=$this->facebookconnect->connect();
            $vars['facebook'] = $facebook;
            $vars['user'] = $facebook->getUser();
            $this->load->model('Users_Model'); 
            if($vars['user'] == 0) {
                // user not set, redirect
                redirect('/', 'refresh');
            } else {
                // user already set up. Remove
                $this->load->model('Users_Model'); 
                $this->Users_Model->remove($vars['user']);
            }

            $this->load->view('removed',$vars);
    }

    protected function iterate_profile ($user,$breadcrumb,$item) {

        foreach($item as $key => $value) {
            if(is_array($value)) {
                $this->iterate_profile($user,$key,$value);
            }
            else {
                if($breadcrumb) {
                    //echo '[' . $breadcrumb . '_' . $key . ']= ' . $value . ' <br />';
                    $key = $breadcrumb . '_' . $key;
                }

                if( ! $this->Users_Model->exists($user,$key)) {
                    // does not exist in the database, insert it
                    $this->Users_Model->add($user,$key,$value);
                } else {
                    $this->Users_Model->update($user,$key,$value);
                }
            }
        }
    } 

型号:

class Users_Model extends CI_Model {
protected $_name = 'users';

function add($id,$key,$value) {
    $data = array(
       'id' => $id,
       'name' => $key,
       'value' => $value
    );

    return $this->db->insert($this->_name, $data); 
}
function update($id,$key,$value) {
    $data = array(
       'value' => $value
    );

    $this->db->where(array(
       'id' => $id,
       'name' => $key
    ));
    return $this->db->update($this->_name, $data); 
}

function exists($id,$key=null) {
    if($key == null) {
        $this->db->where(array(
        'id' => $id
        ));
    } else {
        $this->db->where(array(
        'id' => $id,
        'name' => $key
        ));  
    }

    $query = $this->db->get($this->_name);
    if($query->num_rows() > 0) {
        return true;
    }
    return false;
}

function remove($id) {
    $data = array(
       'id' => $id,
    );

    return $this->db->delete($this->_name, $data); 
}

function all() {
    $query = $this->db->get($this->_name);
    $results = array();
    if($query->num_rows() > 0) {
        foreach($query->result() as $row) {
            $results[]=$row;
        }
    } 
    return $results;  
}

}

视野中的表格:

        <form action="./" method="post" accept-charset="utf-8" enctype="multipart/form-data">
              <table>
                    <tr><td><b class="ara">XXXXXXXX</b> <input id="name" type="text" name="uid" value="" size="30"></td></tr>
                    <tr><td><b class="ara">XXXXXXXX</b><input id="dob" type="text" name="date_of_birth" value="" size="30"></td></tr>
                    <tr><td><b class="ara" >XXXXXXXX</b><input id="email" type="text" name="email" value="" size=30></td></tr>
                    <tr><td><b class="ara">XXXXXXXX</b> <input id="tel" type="text" name="telephone" value="" size="30"></td></tr>
                    <tr><td><input type="radio" name="device" value="11" checked><b>XXXXXXXX</b></input>
                                  <input type="radio" name="device" value="305"><b>XXXXXXXX</b></input>
                                  <input type="radio" name="device" value="306"><b>XXXXXXXX</b></input></td></tr>
                    <tr><td><b>XXXXXXXX</b><select name="reason"> 
                    <option value="volvo">XXXXXXXX</option> 
                    <option value="saab">XXXXXXXX</option> 
                    <option value="mercedes">XXXXXXXX</option> 
                    <option value="audi">XXXXXXXX</option></select></td></tr>
                    <tr><td><b>XXXXXXXX</b><select name="game">
                    <option value="chuzzle">XXXXXXXX</option> 
                    <option value="tetris">XXXXXXXX</option> 
                    <option value="speed">XXXXXXXX</option></select></td></tr>
                    <tr><td><input type="file" name="userfile" class="upload" size="30"/></td></tr>
                    <tr><td><span class="ara tandc">XXXXXXXX</span></td><td><input id="checkme" type="checkbox" class="check" name="Check me" value="Submit"/></td></tr>
                    <tr><td><b><input type="button" class="btn" id="send" name="Add me" value="Submit"/></td></tr>
              </table>
        </form>

1 个答案:

答案 0 :(得分:0)

你实际上得到了什么错误?我乍一看的一个问题是表单提交到./,而不是do_upload,因此代码可能永远不会运行?

从index()运行do_upload()并更改它,以便根据成功返回true / false。如果出现错误,请将其保存为类变量:

class Canvas extends CI_Controller {

    private $upload_error = '';

    function __construct() {
    ...

然后在do_upload()中,而不是:

$error = array('error' => $this->upload->display_errors());

你可以这样做:

$this->upload_error = array('error' => $this->upload->display_errors());

index()中,如果调用do_upload()的结果为false,请从$this->upload_error

中读取错误

总的来说,您的do_upload()方法如下所示:

    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $this->upload_error = array('error' => $this->upload->display_errors());

            return false;
        }
        else
        {
            //$data = array('upload_data' => $this->upload->data());
            return true;
        }
    }