Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Here is my homeprofile controller which also contains a function to change text on the same page once you hit 'change' :
- <?php
- class HomeProfile extends CI_Controller
- {
- function HomeProfile()
- {
- parent::__construct();
- $this->load->model("profiles");
- $this->load->model("profileimages");
- $this->load->helper(array('form', 'url'));
- }
- function changetext()
- {
- $username = $this->session->userdata('username');
- $this->profiles->putProfileText($username, $this->input->post("profiletext"));
- redirect('homeprofile/index');
- }
- 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('profileimages', $config);
- //fail show upload form
- if (! $this->upload->do_upload())
- {
- $error = array('error'=>$this->upload->display_errors());
- $this->load->view('upload_form', $error);
- }
- else
- {
- //successful upload so save to database
- $data = array('upload_data' => $this->upload->data());
- // you may want to delete the image from the server after saving it to db
- // check to make sure $data['full_path'] is a valid path
- $image = chunk_split( base64_encode( file_get_contents( $data['full_path'] ) ) );
- $record = array('user' => $user, 'profileimage' => $image);
- if ($this->_exists($user))
- {
- $this->db->update('profileimages', $record)->where('user', $user);
- }
- else
- {
- $this->db->insert('profileimages', $record);
- }
- // get upload_sucess.php from link above
- $this->load->view('upload_success', $data);
- }
- }
- function _exist($user){
- $query = $this->db->select('*')->from('profileimages')->where('profileimage', $user)->limit(1);
- return ($query->num_rows() > 0) ? true : false;
- }
- function displayImage()
- {
- //Retrieve image id from the URI
- $user = $this->uri->segment(3);
- $this->db->select('*')->from('profileimages')->where('user', $user);
- $query = $this->db->get();
- $image = null;
- if ($query->num_rows() > 0){
- $row = $query->row();
- $image = base64_decode($row->profileimage);
- }
- if (!is_null($image)) {
- //need to know the mine type
- // header('Content-Type: image/png');
- header ('Content-Type: image/jpg');
- imagejpeg(imagecreatefromstring($image), null,100);
- }
- else{
- // load a default profile image
- }
- }
- function viewProfile(){
- //load profile detail view for users and display image
- // 18 = change to image id
- echo '<img src="<?=base_url()?>profile/displayImage/18" alt="profile"/>';
- }
- function index()
- {
- $username = $this->session->userdata('username');
- //$user = $this->session->userdata('user');
- $viewData['username'] = $username;
- $viewData['profileText'] = $this->profiles->getProfileText($username);
- //$viewData['profileimage'] = $this->profileimages->getProfileImage($user);
- $this->load->view('shared/header');
- $this->load->view('homeprofile/homeprofiletitle', $viewData);
- $this->load->view('shared/nav');
- $this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
- $this->load->view('shared/footer');
- }
- }
- my homeprofile view page:
- <div id="maincontent">
- <div id="primary">
- <?$error;?>
- <?$profileimage;?>
- <?=form_open_multipart('homeprofile/do_upload');?>
- <input type="file" name="userfile" size="20" />
- <?=form_submit('submit', 'Upload')?>
- <?=form_close();?>
- </div>
- <div id="secondary">
- <p>
- <?=$profileText;?>
- </p>
- <p>
- <?=form_open('homeprofile/changetext'); ?>
- <?php $msgbox = array(
- 'name' => 'profiletext',
- 'rows' => '8',
- 'cols' => '30',
- );?>
- <?=form_textarea($msgbox);?>
- </p>
- <p>
- <?=form_submit('submit', 'Change'); ?>
- <?=form_close(); ?>
- </p>
- </div>
- </div>
- I'm hoping to pass the image to the same page once I press the upload and appear within the 'profileimage' container - id rather not use the seperate upload_success page
- Currently when I select an image it gives the error Unable to load the requested class: profileimages despite the profileimages model existing - btw I havent changed the model file since:
- hp
- class ProfileImages extends CI_Model
- {
- function ProfileImages()
- {
- parent::__construct();
- }
- function putProfileImage($user, $image)
- {
- $record = array('user' => $user, 'profileimage' => $image);
- if ($this->exists($user))
- {
- $this->db->update('profileimages', $record)->where('user', $user);
- }
- else
- {
- $this->db->insert('profileimages', $record);
- }
- }
- function getProfileImage($user)
- {
- $this->db->select('*')->from('profileimages')->where('user', $user);
- $query = $this->db->get();
- if ($query->num_rows() > 0){
- $row = $query->row();
- return $row->profileimage;
- }
- return Null;
- }
- }
- I appreciate your help so far.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement