1

我正在尝试向我的项目添加一个包含已上传文件的 URL 的最终字段,如果有人能指出我扩展我的模型的写入方向吗?我已经包含了我的 codeIgniter 模型和控制器代码。

控制器:

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

}

function index() {
            $vars = array();
            //$this->load->library('ChromePhp');
            //$this->ChromePhp->log('test');     
            //$this->ChromePhp->log($_SERVER);

            // using labels
           // foreach ($_SERVER as $key => $value) {
             //   $this->ChromePhp->log($key, $value);
          //  }
            // warnings and errors
            //$this->ChromePhp->warn('this is a warning');
            //$this->ChromePhp->error('this is an error');
            $this->load->library('FacebookConnect');
            $facebook=$this->facebookconnect->connect();
            $vars['facebook'] = $facebook;
            $user = $vars['user'] = $facebook->getUser();
            $this->load->model('Users_Model');
            // user already set up. Show thank you page
            if($user != 0  && sizeof($_POST)>0) {
                $this->do_upload();
                $this->iterate_profile ($vars['user'],false,$_POST);
                $this->load->view('canvas',$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'] = '20048';
        $config['max_width']  = '10624';
        $config['max_height']  = '10268';
        $config['file_name']  = date("Y_m_d H:i:s");

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

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

        }
        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;  
}


}
4

1 回答 1

0

我认为这个问题不是很清楚。但对我来说,您似乎需要将图像路径存储在数据库中。否则,您将需要在上传路径上执行读取目录,然后对图像执行某些操作。

于 2013-01-30T13:48:12.637 回答