Advertisement
James_B

Untitled

Oct 29th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.25 KB | None | 0 0
  1. Here is my homeprofile controller which also contains a function to change text on the same page once you hit 'change' :
  2.  
  3. <?php
  4. class HomeProfile extends CI_Controller
  5. {
  6.  
  7.  
  8. function HomeProfile()
  9. {
  10. parent::__construct();
  11. $this->load->model("profiles");
  12. $this->load->model("profileimages");
  13. $this->load->helper(array('form', 'url'));
  14. }
  15.  
  16. function changetext()
  17. {
  18. $username = $this->session->userdata('username');
  19. $this->profiles->putProfileText($username, $this->input->post("profiletext"));
  20. redirect('homeprofile/index');
  21. }
  22.  
  23. function do_upload()
  24. {
  25. $config['upload_path'] = './uploads/';
  26. $config['allowed_types'] = 'gif|jpg|png';
  27. $config['max_size'] = '100';
  28. $config['max_width'] = '1024';
  29. $config['max_height'] = '768';
  30.  
  31. $this->load->library('profileimages', $config);
  32. //fail show upload form
  33. if (! $this->upload->do_upload())
  34. {
  35. $error = array('error'=>$this->upload->display_errors());
  36.  
  37. $this->load->view('upload_form', $error);
  38. }
  39.  
  40. else
  41. {
  42. //successful upload so save to database
  43. $data = array('upload_data' => $this->upload->data());
  44. // you may want to delete the image from the server after saving it to db
  45. // check to make sure $data['full_path'] is a valid path
  46. $image = chunk_split( base64_encode( file_get_contents( $data['full_path'] ) ) );
  47.  
  48.  
  49. $record = array('user' => $user, 'profileimage' => $image);
  50.  
  51. if ($this->_exists($user))
  52. {
  53. $this->db->update('profileimages', $record)->where('user', $user);
  54. }
  55. else
  56. {
  57. $this->db->insert('profileimages', $record);
  58. }
  59. // get upload_sucess.php from link above
  60. $this->load->view('upload_success', $data);
  61. }
  62.  
  63. }
  64.  
  65.  
  66. function _exist($user){
  67. $query = $this->db->select('*')->from('profileimages')->where('profileimage', $user)->limit(1);
  68. return ($query->num_rows() > 0) ? true : false;
  69.  
  70. }
  71.  
  72.  
  73. function displayImage()
  74. {
  75. //Retrieve image id from the URI
  76. $user = $this->uri->segment(3);
  77. $this->db->select('*')->from('profileimages')->where('user', $user);
  78. $query = $this->db->get();
  79. $image = null;
  80. if ($query->num_rows() > 0){
  81. $row = $query->row();
  82. $image = base64_decode($row->profileimage);
  83. }
  84.  
  85.  
  86. if (!is_null($image)) {
  87. //need to know the mine type
  88. // header('Content-Type: image/png');
  89.  
  90. header ('Content-Type: image/jpg');
  91. imagejpeg(imagecreatefromstring($image), null,100);
  92. }
  93. else{
  94. // load a default profile image
  95. }
  96. }
  97.  
  98. function viewProfile(){
  99. //load profile detail view for users and display image
  100. // 18 = change to image id
  101. echo '<img src="<?=base_url()?>profile/displayImage/18" alt="profile"/>';
  102.  
  103. }
  104.  
  105.  
  106. function index()
  107. {
  108. $username = $this->session->userdata('username');
  109. //$user = $this->session->userdata('user');
  110.  
  111. $viewData['username'] = $username;
  112. $viewData['profileText'] = $this->profiles->getProfileText($username);
  113. //$viewData['profileimage'] = $this->profileimages->getProfileImage($user);
  114.  
  115. $this->load->view('shared/header');
  116. $this->load->view('homeprofile/homeprofiletitle', $viewData);
  117. $this->load->view('shared/nav');
  118. $this->load->view('homeprofile/homeprofileview', $viewData, array('error' => ' ' ));
  119. $this->load->view('shared/footer');
  120. }
  121.  
  122. }
  123.  
  124. my homeprofile view page:
  125.  
  126. <div id="maincontent">
  127. <div id="primary">
  128. <?$error;?>
  129. <?$profileimage;?>
  130. <?=form_open_multipart('homeprofile/do_upload');?>
  131. <input type="file" name="userfile" size="20" />
  132. <?=form_submit('submit', 'Upload')?>
  133. <?=form_close();?>
  134. </div>
  135. <div id="secondary">
  136. <p>
  137. <?=$profileText;?>
  138. </p>
  139. <p>
  140. <?=form_open('homeprofile/changetext'); ?>
  141. <?php $msgbox = array(
  142. 'name' => 'profiletext',
  143. 'rows' => '8',
  144. 'cols' => '30',
  145. );?>
  146. <?=form_textarea($msgbox);?>
  147. </p>
  148. <p>
  149. <?=form_submit('submit', 'Change'); ?>
  150. <?=form_close(); ?>
  151. </p>
  152. </div>
  153. </div>
  154.  
  155.  
  156. 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
  157.  
  158. 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:
  159.  
  160. hp
  161. class ProfileImages extends CI_Model
  162. {
  163. function ProfileImages()
  164. {
  165. parent::__construct();
  166. }
  167.  
  168. function putProfileImage($user, $image)
  169. {
  170.  
  171.  
  172. $record = array('user' => $user, 'profileimage' => $image);
  173. if ($this->exists($user))
  174. {
  175. $this->db->update('profileimages', $record)->where('user', $user);
  176. }
  177. else
  178. {
  179. $this->db->insert('profileimages', $record);
  180. }
  181. }
  182.  
  183. function getProfileImage($user)
  184. {
  185. $this->db->select('*')->from('profileimages')->where('user', $user);
  186. $query = $this->db->get();
  187. if ($query->num_rows() > 0){
  188. $row = $query->row();
  189. return $row->profileimage;
  190. }
  191.  
  192. return Null;
  193.  
  194.  
  195. }
  196.  
  197. }
  198.  
  199. I appreciate your help so far.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement