php - How to upload photo in a folder and save path in the database using CodeIgniter -
im trying add image folder , path database i've tried figure out how go nothing working. please help. here's controller
function index() { $this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean'); $this->form_validation->set_rules('photo', 'photo', 'required|trim|xss_clean'); $this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean'); $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>'); if ($this->form_validation->run() == false) // validation hasn't been passed { $this->load->view('registration'); } else // passed validation proceed post success logic { // build array model $form_data = array( 'username' => set_value('username'), 'photo' => set_value('photo'), 'password' => set_value('password') ); // run insert model write data db if ($this->kint_model->saveform($form_data) == true) // information has therefore been saved in db { redirect('user_registration/success'); // or whatever logic needs occur } else { echo 'an error occurred saving information. please try again later'; // or whatever error handling necessary } } } function success() { echo 'user registered successfully'; }
here's model
class kint_model extends ci_model { function __construct() { parent::__construct(); } function saveform($form_data) { $this->db->insert('users', $form_data); if ($this->db->affected_rows() == '1') { return true; } return false; } }
and here's view
<?php // change css classes suit needs $attributes = array('class' => '', 'id' => ''); echo form_open('user_registration', $attributes); ?> <p> <label for="username">username <span class="required">*</span></label> <?php echo form_error('username'); ?> <br /><input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>" /> </p> <p> <label for="photo">photo <span class="required">*</span></label> <?php echo form_error('photo'); ?> <br /><input id="photo" type="text" name="photo" value="<?php echo set_value('photo'); ?>" /> </p> <p> <label for="password">password <span class="required">*</span></label> <?php echo form_error('password'); ?> <br /><input id="password" type="text" name="password" value="<?php echo set_value('password'); ?>" /> </p> <p> <?php echo form_submit( 'submit', 'submit'); ?> </p> <?php echo form_close(); ?>
controller:
function index() { $this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean'); $this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean'); $this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>'); if ($this->form_validation->run() == false) // validation hasn't been passed { $this->load->view('registration'); } else { $form_data = array( 'username' => $this->input->post('username'), 'photo' => '', 'password' => $this->input->post('password') ); $uploadpreferences = array( 'upload_path' => './uploads/fullsize', 'allowed_types' => 'gif|jpg|png', 'encrypt_name' => true ); $this->upload->initialize($uploadpreferences); $this->upload->do_upload('photo'); $photoinfo = $this->upload->data(); $form_data['photo'] = $photoinfo['full_path']; if ($this->kint_model->saveform($form_data) == true) // information has therefore been saved in db { redirect('user_registration/success'); // or whatever logic needs occur } else { echo 'an error occurred saving information. please try again later'; // or whatever error handling necessary } } } function success() { echo 'user registered successfully'; }
view:
<?php // change css classes suit needs $attributes = array('class' => '', 'id' => ''); echo form_open('user_registration', $attributes); ?> <p> <label for="username">username <span class="required">*</span></label> <?php echo form_error('username'); ?> <br /><input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>" /> </p> <p> <label for="photo">photo <span class="required">*</span></label> <?php echo form_error('photo'); ?> <br /><input type="file" name="photo" multiple="multiple" /> </p> <p> <label for="password">password <span class="required">*</span></label> <?php echo form_error('password'); ?> <br /><input id="password" type="text" name="password" value="<?php echo set_value('password'); ?>" /> </p> <p> <?php echo form_submit( 'submit', 'submit'); ?> </p> <?php echo form_close(); ?>
it untested.
read also:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
do : var_dump($this->upload->data());
also.
Comments
Post a Comment