Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter: how to get a filename of a file

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!

like image 442
ignatius nash Avatar asked Mar 28 '13 14:03

ignatius nash


People also ask

How to get fileName in php codeigniter?

$fileExt = pathinfo($fileName, PATHINFO_EXTENSION); print_r($fileExt);

What is $_ files in php?

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.

What is file helper in codeigniter?

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.

What is Writepath in codeigniter 4?

WRITEPATH will give you the path to your uploads folder. You can also apply some validation in the controller when calling your images.


3 Answers

You can get file name like this

  $upload_data = $this->upload->data(); 
  $file_name =   $upload_data['file_name'];
like image 150
It worked yesterday. Avatar answered Nov 04 '22 16:11

It worked yesterday.


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.

like image 44
Marc Audet Avatar answered Nov 04 '22 14:11

Marc Audet


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,
                    ...
                );
}
like image 2
dungthantai Avatar answered Nov 04 '22 16:11

dungthantai