Search code examples
phpcodeigniterimage-uploadingcodeigniter-3profile-picture

How can a user upload a profile picture on my codeigniter registration form


I made a user registration form on the CodeIgniter framework so users can registrate to my website. Now the only thing that doesn't work is that I cant upload a profile picture. When I click on the register button I'm getting 2 errors. I want the profile picture to be uploaded in the product_foto column.

This is my view file: (register.php) :

<form action="" method="POST" enctype='multipart/form-data'>



            <div class="form-group">
                <label for="voornaam" >Voornaam</label>
                <input class="form-control" name="voornaam" id="voornaam" type="text">
            </div>


            <div class="form-group">
                <label for="achternaam">Achternaam</label>
                <input class="form-control" name="achternaam" id="achternaam" type="text">
            </div>

            <div class="form-group">
                <label for="achternaam">Straat en huisnummer</label>
                <input class="form-control" name="straat" id="straat" type="text">
            </div>

            <div class="form-group">
                <input class="form-control" name="huisnummer" id="huisnummer" type="text">
            </div>





            <div class="form-group">
                <label for="huisnummer">Huisnummer</label>
                <input class="form-control" name="huisnummer" id="huisnummer">
            </div>


            <div class="form-group">
                <label for="postcode" >Postcode</label>
                <input class="form-control" name="postcode" id="postcode">
             </div>


            <div class="form-group">
                <label for="woonplaats" >Woonplaats</label>
                <input class="form-control" name="woonplaats" id="woonplaats">
            </div>

            <div class="form-group">
                <label for="email" >Email adres</label>
                <input class="form-control" name="email" id="email" type="emai">
            </div>

            <div class="form-group">
                <label for="wachtwoord" >Wachtwoord</label>
                <input class="form-control" name="wachtwoord" id="wachtwoord" type="password">
            </div>


                <div class="form-group">
                <label for="wachtwoord">Herhaal wachtwoord</label>
                <input class="form-control" name="wachtwoord2" id="wachtwoord" type="password">
            </div>

           <div class="form-group">
                <label for="profiel_foto">Profiel foto</label>
                <input class="form-control" type="file" name="profiel_foto" id="profiel_foto">
            </div> 

            <div class="form-group">
                <label for="beschrijving">Beschrijving</label>
                <input class="form-control" name="beschrijving" id="beschrijving">
            </div>


            <div class="form-group">
                <label for="geboortedatum" >Geboortedatum</label>
                <input class="form-control" name="geboortedatum" id="geboortedatum" type="date">
            </div>


            <div class="form-group">
                <label for="geslacht" >Geslacht</label>
                <select class="form-control" id="geslacht" name="geslacht">
                    <option value="Man">Man</option>   
                    <option value="Vrouw">Vrouw</option>   
                </select>
            </div>

            <div>
                <button class="btn btn-primary" name="register" >Registreren</button>
            </div>
            </form>

This is the register code in the controller:

public function register()
    {




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

         $image = $data_upload_files['./upload/'];


                //voeg gebruiker toe aan database
                $data = array (
                    'voornaam'=>$_POST['voornaam'],
                    'achternaam'=>$_POST['achternaam'],
                    'email'=>$_POST['email'],
                    'wachtwoord'=>  ($_POST['wachtwoord']),
                    'startdatum'=>date('Y-m-d'),
                    'postcode'=>$_POST['postcode'],
                    'huisnummer'=>$_POST['huisnummer'],
                    'woonplaats'=>$_POST['woonplaats'],
                    'beschrijving'=>$_POST['beschrijving'],
                    'geboortedatum'=>$_POST['geboortedatum'],
                    'geslacht'=>$_POST['geslacht'],
                    'profiel_foto'=>$image
                    );
                $this->db->insert('users',$data);

                $this->session->set_flashdata("success", "Uw account is nu geregistreerd, u kunt nu inloggen");
                redirect("auth/register", "refresh");
            }
        }

And these are the 2 errors I'm getting when I'm trying to registrate:

A PHP Error was encountered

Severity: Notice

Message: Undefined index: ./upload/

Filename: controllers/Auth.php

Line Number: 131

Backtrace:

File: /home/ubuntu/workspace/application/controllers/Auth.php
Line: 131
Function: _error_handler

File: /home/ubuntu/workspace/index.php
Line: 315
Function: require_once




A Database Error Occurred

Error Number: 1048

Column 'profiel_foto' cannot be null

INSERT INTO `users` (`voornaam`, `achternaam`, `email`, `wachtwoord`, `startdatum`, `postcode`, `huisnummer`, `woonplaats`, `beschrijving`, `geboortedatum`, `geslacht`, `profiel_foto`) VALUES ('hallo', 'hallo', '[email protected]', 'hallo', '2017-06-28', 'hallo', 'hallo', 'hallo', 'hallo', '2017-06-10', 'Man', NULL)

Filename: controllers/Auth.php

Line Number: 149

Solution

  • replace

    $config['upload_path'] = './upload/';
             $config['allowed_types'] = 'gif|jpg|png|jpeg';
             $this->load->library('upload', $config);
             $this->input->post('profiel_foto');
             $data_upload_files = $this->upload->data();
    
             $image = $data_upload_files['./upload/'];
    

    With

                        $target_dir = "upload/";
                        $target_file = $target_dir . time().basename($_FILES["profiel_foto"]["name"]);
                        $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
                        $imgName = time().basename($_FILES["profiel_foto"]["name"]);
                        move_uploaded_file($_FILES["profiel_foto"]["tmp_name"], $target_file);
    

    Your insert function

    $data = array (
                        'voornaam'=>$_POST['voornaam'],
                        'achternaam'=>$_POST['achternaam'],
                        'email'=>$_POST['email'],
                        'wachtwoord'=>  ($_POST['wachtwoord']),
                        'startdatum'=>date('Y-m-d'),
                        'postcode'=>$_POST['postcode'],
                        'huisnummer'=>$_POST['huisnummer'],
                        'woonplaats'=>$_POST['woonplaats'],
                        'beschrijving'=>$_POST['beschrijving'],
                        'geboortedatum'=>$_POST['geboortedatum'],
                        'geslacht'=>$_POST['geslacht'],
                        'profiel_foto'=>$imgName
                        );
                    $this->db->insert('users',$data);