0

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

下面是我的视图中的控制器、模型和表单,有人可以帮我补上缺失的链接吗?我已经搜索了所有并没有找到解决方案

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

1 回答 1

0

你实际上得到了什么错误?我第一眼看到的一个问题是表单提交给./, not 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()为假,则从$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;
        }
    }
于 2012-09-19T11:08:12.053 回答