CodeIgniter Forums
File type returning NULL - Did actually work but I don't know what I have done. Help! - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: General (https://forum.codeigniter.com/forum-1.html)
+--- Forum: Lounge (https://forum.codeigniter.com/forum-3.html)
+--- Thread: File type returning NULL - Did actually work but I don't know what I have done. Help! (/thread-71147.html)



File type returning NULL - Did actually work but I don't know what I have done. Help! - dreamweaver - 07-12-2018

Before the breakdown the view was returning a string and storing that string in a database. Since its my first time trying a project, a run into a problem where previously it worked, and then after it just decided to stop working. 

The Error Message: 
A PHP Error was encountered
Severity: Notice
Message: Undefined index: picture
Filename: models/Horse_model.php
Line Number: 23
Backtrace:
File: C:\xampp\htdocs\rusaliite\application\models\Horse_model.php
Line: 23
Function: _error_handler

File: C:\xampp\htdocs\rusaliite\application\controllers\Horse_controller.php
Line: 27
Function: create

File: C:\xampp\htdocs\rusaliite\index.php
Line: 315
Function: require_once


The View:
<div id="body">
    <?php echo validation_errors(); ?>
        <?php echo form_open_multipart('horse_controller/create'); ?>
            <form style="width: 30%;">
                <div class="form-group">
                    <label>Name</label>
                    <input name="name" class="form-control" maxlength="20">
                </div>
                <div class="form-group">
                    <label>Bulgarian Name</label>
                    <input name="name_bulgarian" class="form-control" maxlength="20">
                </div>
                <div class="form-group">
                    <label>Sex</label>
                    <input name="sex" class="form-control" maxlength="1" placeholder="M or F">
                </div>
                <div class="form-group">
                    <label>Stable Number</label>
                    <input name="stable_no" class="form-control" maxlength="2" type="number">
                </div>
                <div class="form-group">
                    <label>Picture</label>
                    <input name="picture" type="file" class="form-control-file">
                </div>
                <button type="submit" method="POST" class="btn btn-primary">Submit</button>
            </form>
        </div>


The Controller:
public function create()
    {
        if($_SERVER['REQUEST_METHOD'] == 'POST')
        {   
            // $config['upload_path']          = './uploads/';
            // $config['allowed_types']        = '';
            // $config['max_size']             = 0;
            // $config['max_width']            = 0;
            // $config['max_height']           = 0;
    
            // $this->load->library('upload', $config);
            // $this->upload->do_upload('photo');

            $data['horses'] = $this->Horse_model->create();

            $this->load->view('templates/header');
            redirect('horse_controller/index');
            $this->load->view('templates/footer');
        }
        else
        {
            $this->load->view('templates/header');
            $this->load->view('horse/create');
            $this->load->view('templates/footer');
        }
    }


The Model:
   public function create()
   {
       $this->name             = $_POST['name'];
       $this->name_bulgarian   = $_POST['name_bulgarian'];
       $this->sex              = $_POST['sex'];
       $this->stable_no        = $_POST['stable_no'];
       $this->picture          = $_POST['picture'];

       $this->db->insert('horse', $this);
   }



RE: File type returning NULL - Did actually work but I don't know what I have done. Help! - Pertti - 07-12-2018

Uploaded files handle differently from uploaded form data.

You have form_open_multipart function call followed by another form tag, that will generate two forms inside each other and submit will only work in inner form, so that might cause some issues.

Also, all type="file" go into $_FILES instead of $_POST variable.

More details on how to handle uploads with CodeIgniter at https://www.codeigniter.com/userguide3/libraries/file_uploading.html
Also, for raw PHP approach, you can read up how it works here http://php.net/manual/en/features.file-upload.post-method.php


RE: File type returning NULL - Did actually work but I don't know what I have done. Help! - dreamweaver - 07-12-2018

Hey I wrote before but I don't see it, I'll say it again just incase there was some problem. 

I had the thing working, it was returning the string of the file, which I want to store in the database and then I can use this to reference the picture I want to use in the file. 

The whole thing was totally working now, its not. Totally head dead, after looking at it over and over again.

The $_File works but it doesn't return the string value, which I want to store in the database.


RE: File type returning NULL - Did actually work but I don't know what I have done. Help! - dreamweaver - 07-12-2018

Hey, thanks, so looking further into it I used $_POST['file]['name]. I have total brain drain from this little problem, I have no idea why before it worked and it decided to stop.