2

I have created a simple contact form.I want to store the file in database which is uploaded by any user.I am able to store the file in my folder but i have to store the file in database to identify the admin which file is upload.

Controller

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

 public function do_upload()
        {


                $id = $this->session->userdata('id');
                $this->load->model('Model_doc');

                $config['upload_path']          = './upload/';
                $config['allowed_types']        = 'docx';
                $config['max_size']             = 1000;
                $config['max_width']            = 1024;
                $config['max_height']           = 768;

                $this->load->library('upload', $config);

                $this->upload->do_upload();
                $this->Model_doc->index($id, $this->upload->data());
   } 

Model

public function index($id,$imgdata)

{


 $imgdata = file_get_contents($imgdata['../upload/']);

$data = array(
        'path'=>$imgdata,

         );
    $this->db->set('curentDate', 'NOW()', FALSE);
 $this->db->insert('test',$data);

}

View

<?php echo form_open_multipart('welcome/do_upload');?>

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

<br /><br />

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

</form>
1
  • i am getting 2 error in model 1) Undefined index: ../upload/ 2)file_get_contents(): Filename cannot be empty. Commented Feb 9, 2016 at 3:03

1 Answer 1

1

Change code in the Controller File as Folow:

public function do_upload()
    {
        $id = $this->session->userdata('id');
        $this->load->model('Model_doc');
        $config['upload_path']          = './upload/';
        $config['allowed_types']        = 'docx';
        $config['max_size']             = 1000;
        $config['max_width']            = 1024;
        $config['max_height']           = 768;
        $this->load->library('upload', $config);
        $this->upload->do_upload();

        $full_file_path = base_url()."/upload/".$_FILES['userfile']['name'];
        //This Is Full Path of the file stored to your folder,Change it if required.

        $this->Model_doc->index($id,$full_file_path);
    }

Changes in the Model File:

public function index($id,$full_file_path)
    {

        $data = array(
            'path'=>$full_file_path,
        );
        $this->db->set('curentDate', 'NOW()', FALSE);
        $this->db->insert('test',$data);
    }
Sign up to request clarification or add additional context in comments.

6 Comments

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head> <title>Index of /newfolder//upload</title> </head> <body> <h1>Index of /newfolder//upload</h1> <table> <tr><th valign=
Thanks for reply Mr.ketan , When i am submit the data i am getting above code in my database....
@Hybreeder Do you need the path to be stored in the database or just the file name,Because File_get_contents reads the file as a string from the mentioned starting point.
@Hybreeder Check the code above , I have updated the code.
@Hybreeder Keep Rocking ...:)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.