mercredi 25 mars 2015

Inserting file_path into database using Codeigniter


I am new to Codeigniter, so having some confusing on this proccess: I have a form field where user can upload any jpg|png|gif file ,the uploaded file is successfully sent to a uploads directory and a success message is displayed. But how will I get the file_path of that particular file uploaded and insert it into database.


the code below works fine until I call the model:



`$this->upload_model->upload_path();`


so definately there is a problem with my model ,cant figure out.


This is my Controller:



class Upload extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->model('upload_model');
}

public function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}

public 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('upload', $config);

if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());

$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());

// when the code below is uncommented it shows error,otherwise runs
// $this->upload_model->upload_path();

$this->load->view('upload_success', $data);

}
}
}


This is my model(which shows error):



class Upload_model extends CI_Model {

// this function will be called instantenneously

public function __construct()
{
parent::__construct();

$this->load->database();
}
public function upload_path(){

// ::::::::UNDER CONSTRUCTION::::::::::

$data = array(
'path' =>$upload_data['full_path'],

);

return $this->db->insert('upload', $data);


}

}


the View:



<html>
<head>
<title>Upload Form</title>
</head>
<body>

<?php echo $error;?>

<form method="POST" action="/sandeep/ci/http://ift.tt/VNUqCF" enctype="multipart/form-data" />

<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>




Aucun commentaire:

Enregistrer un commentaire