i am a Codeigniter rookie and i am trying to get the file name of an uploaded image so that i can save it in the database. i have two models, homemodel deals with my database and the image_upload_model deals with image uploading. Everything works fine except i dont know how to post the image filename to the database
image_upload_model.php
<?php
class Image_upload_model extends CI_Model {
var $image_path;
//constructor containing the image path
function Image_upload_model() {
$this->image_path = realpath(APPPATH.'../assets/img');
}
//uploading the file
function do_upload() {
$config = array(
'allowed_types' => 'jpg|jpeg|gif|png',
'upload_path' => $this->image_path
);
$this->load->library('upload',$config);
$this->upload->do_upload();
}
}
?>
homemodel.php
<?php
class homeModel extends CI_Model {
//inserting into the table tenants
function addTenants() {
$this->load->model('Image_upload_model');
$data = array(
'Fname' => $this->input->post('Fname'),
'Lname' => $this->input->post('Lname'),
'contact' => $this->input->post('contact'),
'email' => $this->input->post('email'),
'location' => $this->input->post('location'),
'img_url' => " "//the filename of the image should go here
);
$this->db->insert('tenants', $data);
}
}
?>
The controller
homecontroller.php
<?php
class HomeController extends CI_Controller {
public function index() {
$this->load->helper('form');
$this->load->helper('html');
$this->load->model('homemodel');
$this->load->model('Image_upload_model');
if ($this->input->post('submit') == 'Submit') {
$this->homemodel->addTenants();
echo 'inserted';
$this->Image_upload_model->do_upload();
echo 'image uploaded';
}
$this->load->view('index.php');
}
}
?>
any help is appreciated, thank you!
$fileExt = pathinfo($fileName, PATHINFO_EXTENSION); print_r($fileExt);
PHP $_FILES The global predefined variable $_FILES is an associative array containing items uploaded via HTTP POST method. Uploading a file requires HTTP POST method form with enctype attribute set to multipart/form-data.
They are simple, procedural functions. Each helper function performs one specific task, with no dependence on other functions. CodeIgniter does not load Helper Files by default, so the first step in using a Helper is to load it. Once loaded, it becomes globally available in your controller and views.
WRITEPATH will give you the path to your uploads folder. You can also apply some validation in the controller when calling your images.
You can get file name like this
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
On a very high level, you need to restructure your code as follows:
(1) In your HomeController
, upload the image first ($this->Image_upload_model->do_upload()
and then update your database ($this->homemodel->addTenants()
(2) In your upload model, you need to invoke $this->upload->data()
to get the information array that contains your file name (see CodeIgniter documentation). You then have to get that file name and make it available to the HomeController and pass it to the addTenants
function.
With this guidance, you should be able to modify your code accordingly.
Easy to get file name with $this->upload->file_name
based on function upload in system/library/upload.php
public $file_name = "";
public function data()
{
return array (
'file_name' => $this->file_name,
'file_type' => $this->file_type,
...
);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With