CodeIgniter Forums
IMage upload problem Help needed please - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: IMage upload problem Help needed please (/thread-47618.html)



IMage upload problem Help needed please - El Forum - 12-15-2011

[eluser]the_unforgiven[/eluser]
Hi all i ahev all the following in each files but when it saves to the db it just inserts a 0 instead of the image path just wondered if someone could help me please Smile

Controller
Code:
function createuser()
{
  $data['heading'] = "Registration";

  
  // Validation Rules
  $this->form_validation->set_rules('fname', 'First Name', 'trim|xss_clean|xss_clean');
  $this->form_validation->set_rules('lname', 'Last Name', 'trim|xss_clean|xss_clean');  
  $this->form_validation->set_rules('address', 'Address', 'trim|required|min_length[10]|xss_clean');
  $this->form_validation->set_rules('postcode', 'Postcode', 'trim|required|max_length[7]|xss_clean');
  $this->form_validation->set_rules('phone', 'Phone', 'trim|required|min_length[11]|max_length[11]|xss_clean');
  $this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|xss_clean');
  $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[5]|xss_clean');
  $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]|xss_clean|alpha_numeric');

  
  // Validate required fields  
  if ($this->form_validation->run() == FALSE)
  {

   $data['fail'] = $this->session->set_flashdata('fail', 'Sorry! Something wasn\'t right please correct in order to continue');
   $this->load->view('register', $data);
  }
  else
  {
   $activation_code = $this->_random_string(30);
   $email = $this->input->post('email');
  
   $data = $this->user_model->reg($activation_code);
  
    
   $this->load->library('email');
   $this->email->from('[email protected]', 'COnfirm');
   $this->email->to($email);
   $this->email->subject('Registration Confirmation');
   $this->email->message('Please activate your account by clicking on the link   ' . anchor('http://localhost/cars/user/register_confirm/' . $activation_code . ' ',' '));
    
  
   $this->email->send(); // Send the message function to the persons email address
   $this->email->clear(); // Clears the form ready for another submission
  
   echo '[removed]
       alert("Thank You.\r\n\r\n\r\nPlease check your email to activate your account.\r\nYou can then log into your account.");
       location = "../main/";
   [removed]';

  }
}

Model
Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class User_model extends CI_Model {

var $gallery_path;
var $gallery_path_url;


// Construct Method
function __construct()
{
      parent::__construct();
  $this->gallery_path = realpath(APPPATH . '../uploads');
  $this->gallery_path_url = base_url().'uploads/';
        
}

function reg($activation_code)
{
  $data = array(
    'username'=>$this->input->post('username'),
    'password'=>md5($this->input->post('password')),
       'fname'=>$this->input->post('fname'),
    'lname'=>$this->input->post('lname'),
    'address'=>$this->input->post('address'),
    'postcode'=>$this->input->post('postcode'),
    'email'=>$this->input->post('email'),
    'phone'=>$this->input->post('phone'),
    'activation_code'=> $activation_code,
    'image' => //WHAT SHOULD BE HERE IN ORDER TO GET THE FULL URL OF THE IMAGE?
    
  );
  $this->db->insert('users', $data);
  return;

}
  
function do_upload() {
  
  $config = array(
   'allowed_types' => 'jpg|jpeg|gif|png',
   'upload_path' => $this->gallery_path,
   'max_size' => 2000
  );
  
  $this->load->library('upload', $config);
  $this->upload->do_upload();
  $image_data = $this->upload->data();
  
  $config = array(
   'source_image' => $image_data['full_path'],
   'new_image' => $this->gallery_path . 'uploads/thumbs',
   'maintain_ration' => true,
   'width' => 150,
   'height' => 100
  );
  
  $this->load->library('image_lib', $config);
  $this->image_lib->resize();
  
}

function get_images() {
  
  $files = scandir($this->gallery_path);
  $files = array_diff($files, array('.', '..', 'uploads/thumbs'));
  
  $images = array();
  
  foreach ($files as $file) {
   $images []= array (
    'url' => $this->gallery_path_url . $file,
    'thumb_url' => $this->gallery_path_url . 'uploads/thumbs/' . $file
   );
  }
  
  return $images;
}
}

View
Code:
<?php $atts = array('id' => 'userfile'); ?>
<?php echo form_open_multipart('user/createuser', $atts); ?>
<fieldset>
     <legend>Your Details</legend>
<ol>
     <li><label>First Name:</label> &lt;input type="text" name="fname" size="50" value="" /&gt;&lt;/li>
  <li><label>Last Name:</label> &lt;input type="text" name="lname" size="50" value="" /&gt;&lt;/li>
     <li><label>Address: </label>&lt;textarea name="address" rows="5" cols="20"&gt;&lt;/textarea></li>
     <li><label>Postcode: </label>&lt;input type="text" name="postcode" size="50" value=""/&gt;&lt;/li>
  <li><label>Phone:</label> &lt;input type="text" name="phone" size="50" value=""/&gt;&lt;/li>
     <li><label>Email: </label>&lt;input type="text" name="email" size="50" value=""/&gt;&lt;/li>
    
</ol>
<legend>Login Info</legend>
<ol>
  <li><label>Username:</label> &lt;input type="text" name="username" size="50" value="" /&gt;&lt;/li>
  <li><label>Password:</label> &lt;input type="password" name="password" size="50" value="" /&gt;&lt;/li>
</ol>
<legend>Image</legend>
<ol>
  <li><label>Upload:</label>&lt;?php echo form_upload('userfile'); ?&gt;</li>
</ol>
</fieldset>
&lt;input type="submit" class="submit" value="Submit" /&gt;
&lt;/form&gt;

The attached image is my database for 'users'

Help much appreciated thanks in advance