Advertisement
James_B

Profile Image Upload

Oct 27th, 2012
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. Now getting following error when pressing upload image button:
  2. Unable to load the requested class: profileimages
  3.  
  4. Upload Controller:
  5.  
  6. <?php
  7.  
  8. class Upload extends CI_Controller {
  9.  
  10. function __construct()
  11. {
  12. parent::__construct();
  13. $this->load->model("profileimages");
  14. $this->load->helper(array('form', 'url'));
  15. }
  16.  
  17. function index()
  18. {
  19. $this->load->view('upload_form', array('error' => ' ' ));
  20. }
  21.  
  22. function Upload()
  23. {
  24. $user = $this->session->userdata('user');
  25. $this->profileimages->putProfileImage($user, $this->input->post("profileimage"));
  26. redirect('homeprofile/index');
  27. }
  28.  
  29. function do_upload()
  30. {
  31. $config['upload_path'] = './uploads/';
  32. $config['allowed_types'] = 'gif|jpg|png';
  33. $config['max_size'] = '100';
  34. $config['max_width'] = '1024';
  35. $config['max_height'] = '768';
  36.  
  37. $this->load->library('profileimages', $config);
  38.  
  39. if (! $this->profileimages->do_upload())
  40. {
  41. $error = array('error'=>$this->profileimages->display_errors());
  42.  
  43. $this->load->view('upload_form', $error);
  44. }
  45.  
  46. else
  47. {
  48. $data = array('upload_data' => $this->profileimages->data());
  49.  
  50. $this->load->view('upload_success', $data);
  51. }
  52.  
  53. redirect('homeprofile/index');
  54.  
  55. }
  56.  
  57. function displayImage()
  58. {
  59. //Retrieve image id from the URI
  60. $imageid = $this->uri->segment(3);
  61. //Initialize the images model
  62. $this->load->model("profileimages");
  63. //Call the model's getImage function passing the image id
  64. $image = $this->image_model->getProfileImage($imageid);
  65. if (!is_null($image)) {
  66. //need to know the mine type
  67. // header('Content-Type: image/png');
  68.  
  69. header ('Content-Type: image/jpg');
  70. imagejpeg(imagecreatefromstring($image), null,100);
  71. }
  72. else{
  73. // load a default profile image
  74. }
  75.  
  76. function viewProfile(){
  77. //load profile detail view for users and display image
  78. // 18 = change to image id
  79. echo '<img src="<?=base_url()?>profile/displayImage/18" alt="profile"/>';
  80.  
  81. }
  82.  
  83.  
  84. }
  85.  
  86.  
  87.  
  88.  
  89. }
  90. ?>
  91.  
  92. ProfileImages Model:
  93.  
  94. <?php
  95. class ProfileImages extends CI_Model
  96. {
  97. function ProfileImages()
  98. {
  99. parent::__construct();
  100. }
  101.  
  102. function putProfileImage($user, $image)
  103. {
  104.  
  105.  
  106. $record = array('user' => $user, 'profileimage' => $image);
  107. if ($this->exists($user))
  108. {
  109. $this->db->update('profileimages', $record)->where('user', $user);
  110. }
  111. else
  112. {
  113. $this->db->insert('profileimages', $record);
  114. }
  115. }
  116.  
  117. function getProfileImage($user)
  118. {
  119. $this->db->select('*')->from('profileimages')->where('user', $user);
  120. $query = $this->db->get();
  121. if ($query->num_rows() > 0){
  122. $row = $query->row();
  123. return $row->profileimage;
  124. }
  125.  
  126. return Null;
  127.  
  128.  
  129. }
  130.  
  131. }
  132.  
  133. Homeprofile View:
  134. http://localhost/ci/web-project-jb.php/homeprofile
  135.  
  136.  
  137. <div id="maincontent">
  138. <div id="primary">
  139. <?$error;?>
  140. <?=form_open_multipart('upload/do_upload');?>
  141. <input type="file" name="userfile" size="20" />
  142. <?=form_submit('submit', 'Upload')?>
  143. <?=form_close();?>
  144. </div>
  145. <div id="secondary">
  146. <p>
  147. <?=$profileText;?>
  148. </p>
  149. <p>
  150. <?=form_open('homeprofile/changetext'); ?>
  151. <?php $msgbox = array(
  152. 'name' => 'profiletext',
  153. 'rows' => '8',
  154. 'cols' => '30',
  155. );?>
  156. <?=form_textarea($msgbox);?>
  157. </p>
  158. <p>
  159. <?=form_submit('submit', 'Change'); ?>
  160. <?=form_close(); ?>
  161. </p>
  162. </div>
  163. </div>
  164.  
  165. Upload_success view:
  166. http://localhost/ci/web-project-jb.php/upload/do_upload
  167. I created this file following a tutorial, but ideally I want the image to appear on the same Homeprofile view page once uploaded
  168.  
  169. <html>
  170. <head>
  171. <title>Upload Form</title>
  172. </head>
  173. <body>
  174.  
  175. <h3>Your file was successfully uploaded!</h3>
  176.  
  177. <ul>
  178. <?php foreach ($upload_data as $item => $value):?>
  179. <li><?php echo $item;?>: <?php echo $value;?></li>
  180. <?php endforeach; ?>
  181. </ul>
  182.  
  183. <p><?php echo anchor('upload', 'Upload Another File!'); ?></p>
  184.  
  185. </body>
  186. </html>
  187.  
  188.  
  189. SQL statement to create table - create table profileimages (user varchar(256),
  190. profileimage varchar(50));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement