0

I'm having a bit of a problem trying to make an upload form in codeigniter. Not sure what I am doing wrong, everything else is getting correctly in the database. I looked in the documentation and tried several things but I'm not getting any further.. Any feedback is very much appreciated!
Thanks in advance.

model:

public function set_newstudent()
    {
        $this->load->helper('url');

        $slug = url_title($this->input->post('naam'), 'dash', TRUE);

                 $config['upload_path'] = '/file_path/';
                 $config['allowed_types'] = 'gif|jpg|png|jpeg';
                 $this->load->library('upload', $config);
                 $this->upload->data('full_path'););
                 $data_upload_files = $this->upload->data();

                 $image = $data_upload_files[full_path];

                 $data = array(

            'naam' => $this->input->post('naam'),
            'voornaam' => $this->input->post('voornaam'),
            'id' => $slug,
            'text' => $this->input->post('text'),
            'picture'=>$this->input->post('picture')
    );

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

controller:

public function create()
            {
                $this->load->helper('form');
                $this->load->library('form_validation');

                $data['title'] = 'Create a new Student';

                $this->form_validation->set_rules('naam', 'Naam', 'required');
                $this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
                $this->form_validation->set_rules('text', 'Text', 'required');

                if ($this->form_validation->run() === FALSE)
                    {
                    $this->load->view('templates/header', $data);
                    $this->load->view('students/create');
                    $this->load->view('templates/footer');

                    }
                else
                    {
                    $this->student_model->set_newstudent();
                    $this->load->view('students/success');
                    }
            }

view:

<?php echo validation_errors(); ?>


<?php echo form_open_multipart('student/create');?>

<div class="form-group">
<label for="naam">Naam</label><br>
<input type="input" name="naam" class="form-control" /><br />
</div>
<div class="form-group">
<label for="voornaam">Voornaam</label><br>
<input type="input" name="voornaam" class="form-control"/><br />
</div>
<div class="form-group">
<label for="text">Vertel iets over jezelf:</label><br>
<textarea name="text" class="form-control" rows="5"></textarea><br />
</div>

<div class="form-group">
<label for="text">Kies een profiel foto:</label><br>
<input type="file" name="userfile"  class="btn btn-default btn-file" />

</div>
<input type="submit" class="btn btn-success" name="submit" 
value="Create student" style="width:100%;margin-bottom:1%" />

</form>
6
  • What is this ? $this->upload->create Commented May 7, 2017 at 18:25
  • Sorry sir my mistake, it should be $this->upload->data('full_path'); Commented May 7, 2017 at 18:32
  • What version of codeigniter are you using please ? Commented May 7, 2017 at 18:33
  • Code igniter version is 3.1.4 Commented May 7, 2017 at 18:43
  • What are you trying to insert in the id column ? Commented May 7, 2017 at 19:26

2 Answers 2

4

As i see, there is no

$this->upload->do_upload()

That's the function responsible for performing the upload and it's not there in your code,you may want to read this: File uploading Class

UPDATE

Your code should look something like this,

Controller:

public function create(){
    $this->load->helper('form');
    $this->load->library('form_validation');
    $data['title'] = 'Create a new Student';
    $this->form_validation->set_rules('naam', 'Naam', 'required');
    $this->form_validation->set_rules('voornaam', 'Voornaam', 'required');
    $this->form_validation->set_rules('text', 'Text', 'required');
    if ($this->form_validation->run() === FALSE){
        $this->load->view('templates/header', $data);
        $this->load->view('students/create');
        $this->load->view('templates/footer');
    }else{
        // Upload the files then pass data to your model
        $config['upload_path'] = '/file_path/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $this->load->library('upload', $config);

        if (!$this->upload->do_upload('userfile')){
            // If the upload fails
            echo $this->upload->display_errors('<p>', '</p>');
        }else{
            // Pass the full path and post data to the set_newstudent model
            $this->student_model->set_newstudent($this->upload->data('full_path'),$this->input->post());
            $this->load->view('students/success');
        }
    }
}

Model:

public function set_newstudent($path,$post){ 
    $data = array( 
        'naam' => $post['naam'], 
        'voornaam' => $post['voornaam'], 
        'text' => $post['text'], 
        'picture'=>$path 
    ); 

    return $this->db->insert('student', $data); 
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for your answer sir. Can I add this to the function create() ? or does it have to be a separate function?
There is no such function as create, can you tell me where did you find this function ?
In the controller
No you can't do that, you may want to re-write the code using the example i gave you in the documentation
Add the code i added echo $this->upload->display_errors('<p>', '</p>');
|
0

The problem is in your code where you are using upload library. You are using the wrong function to upload files. Below is the correct code:

        $config['upload_path'] = '/file_path/';
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $this->load->library('upload', $config);

        // Check file uploaded or not
        if ($this->upload->do_upload('userfile')) {
            $data_upload_files = $this->upload->data();
            $image = $data_upload_files[full_path];
        } else {
            // possibly do some clean up ... then throw an error

        }

This code should work.

3 Comments

Sorry it's not working, I changed the code in the controller to the above but i'm still not getting the result
May I know what exact error you are getting on screen?
The thing is, I do not get an error. The user gets created and the fields First and Last name are being written in the database, but I see no picture even though it was attached

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.