用户如何在我的 codeigniter 注册表单上上传个人资料图片

Posted

技术标签:

【中文标题】用户如何在我的 codeigniter 注册表单上上传个人资料图片【英文标题】:How can a user upload a profile picture on my codeigniter registration form 【发布时间】:2017-12-01 22:17:24 【问题描述】:

我在 CodeIgniter 框架上制作了一个用户注册表单,以便用户可以注册到我的网站。现在唯一不起作用的是我无法上传个人资料图片。当我单击注册按钮时,我收到 2 个错误。我希望将头像上传到 product_foto 列中。

这是我的视图文件:(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>

这是控制器中的注册码:

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");
            
        

这是我在尝试注册时遇到的 2 个错误:

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', 'hallo@gmail.com', 'hallo', '2017-06-28', 'hallo', 'hallo', 'hallo', 'hallo', '2017-06-10', 'Man', NULL)

Filename: controllers/Auth.php

Line Number: 149

【问题讨论】:

尝试打印 data_upload_files。检查你得到了什么 什么也没发生我看到同样的错误 【参考方案1】:

我修改了你的代码。试试这个

public function register() 
  $data = array();

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

  if (!$this->upload->do_upload('profiel_foto')) 
    $error = array('error' => $this->upload->display_errors());
    // var_dump( $error); die; check errors 
   else 
    $fileName = $this->upload->data();
    $data['profiel_foto'] = $fileName['file_name'];
  
  // 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'],
  );
  $this->db->insert('users', $data);

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

【讨论】:

【参考方案2】:

替换

$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/'];

                    $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);

你的插入函数

$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);

【讨论】:

不工作我现在收到此错误:遇到 PHP 错误严重性:通知消息:未定义变量:图像文件名:控制器/Auth.php 行号:146 回溯:文件:/home/ubuntu/ workspace/application/controllers/Auth.php 行:146 函数:_error_handler 文件:/home/ubuntu/workspace/index.php 行:315 函数:require_once 它仍然无法正常工作,我收到此错误:遇到 PHP 错误严重性:通知消息:未定义变量:imageName 文件名:控制器/Auth.php 行号:129 回溯:文件:/home/ ubuntu/workspace/application/controllers/Auth.php 行:129 函数:_error_handler 文件:/home/ubuntu/workspace/index.php 行:315 函数:require_once $imgName替换$imageName

以上是关于用户如何在我的 codeigniter 注册表单上上传个人资料图片的主要内容,如果未能解决你的问题,请参考以下文章

如何修复我的表单没有将数据更新到数据库 [Codeigniter]

CodeIgniter - AJAX 注册表单验证

如何使用 ajax (codeigniter) 在我的编辑表单中获取和显示选定值到 <select2> 标记中

Codeigniter将数据库值分配给变量

表单验证在我的 CI 项目中不起作用(已修复,代码已更新)

php codeigniter - 表单助手 - 如何在无线电输入上实现 set_value