【问题标题】:How to set a default image in if user not defined in codeigniter如果用户未在 codeigniter 中定义,如何设置默认图像
【发布时间】:2016-05-18 23:01:11
【问题描述】:

我的控制器是,但是如果用户在表单中输入他/她的详细信息时没有上传任何图像,我如何在数据库中插入默认图像名称。

<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

class Student extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('student_model');
        $this->load->model('hostelinfo_model');
        $this->load->model('options_model');
        $this->load->model('upload_model');
        $this->load->library('form_validation');
        $this->load->library('upload');
    }

    public function index() {
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
        $this->form_validation->set_rules('student_id', 'Student ID', 'required|trim|xss_clean|callback_student_id_check');
        $this->form_validation->set_rules('student_name', 'Student Name', 'required|trim|xss_clean');
        $this->form_validation->set_rules('father_name', 'Father Name', 'required|trim|xss_clean');
        $this->form_validation->set_rules('program_name', 'Progrem Name', 'required|trim|xss_clean');
        $this->form_validation->set_rules('mobile', 'Mobile No', 'required|trim|xss_clean|is_unique[student.mobile]|max_length[11]|min_length[11]');
        $this->form_validation->set_rules('email', 'Email ID', 'trim|xss_clean|is_unique[student.email]|valid_email');
        $this->form_validation->set_rules('hostel_name', 'Hostel Name', 'required|trim|xss_clean');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('header1');
            $this->load->view('book_hostel');
            $this->load->view('footer');
        } else {
           $config['upload_path'] = 'uploads';
           $config['allowed_types'] = 'jpeg|jpg|png';
           $config['max_size'] = '2048';
           $config['encrypt_name'] = true;
           $this->upload->initialize($config);
           if (!$this->upload->do_upload('image_name')) {
               $image_data = "noimage.png";
               $data['error'] = $this->upload->display_errors();
                   $this->load->view('header');
                   $this->load->view('book_hostel', $data);
                   $this->load->view('footer');
            }else{
               $this->form_validation->set_rules('image_name', 'Upload Image', 'trim|xss_clean');
               if ($this->form_validation->run($this) == FALSE) {
                   $data['error'] = '';
                   $this->load->view('header1');
                   $this->load->view('book_hostel', $data);
                   $this->load->view('footer');
                }
            }
                $image_data = $this->upload->data();
                $data = array(
                'image_name' => $image_data['file_name'],
                'student_id' => $this->input->post('student_id'),
                'student_name' => $this->input->post('student_name'),
                'father_name' => $this->input->post('father_name'),
                'program_name' => $this->input->post('program_name'),
                'mobile' => $this->input->post('mobile'),
                'email' => $this->input->post('email'),
                'hostel_name' => $this->input->post('hostel_name')

            );
            $this->student_model->form_insert($data);
            //$this->sendEmail();
            $data['message'] = 'Data Inserted Successfully';
            redirect('hostel');

         }
    }
}

【问题讨论】:

    标签: php image codeigniter upload


    【解决方案1】:

    首先检查用户是否没有上传任何图片

        $default_image=0;
    
        if($_FILES['image_name']['tmp_name'] != '') {
              if (!$this->upload->do_upload('image_name')) {
                     $default_image =1;
                    }else{
                       $this->form_validation->set_rules('image_name', 'Upload Image', 'trim|xss_clean');
                       if ($this->form_validation->run($this) == FALSE) {
                           $data['error'] = '';
                           $this->load->view('header1');
                           $this->load->view('book_hostel', $data);
                           $this->load->view('footer');
                        }
                    }
        }else {
           $default_image=1;
        }
    
       if($default_image) {
               $image_data = "noimage.png";
               $data['error'] = $this->upload->display_errors();
               $this->load->view('header');
               $this->load->view('book_hostel', $data);
               $this->load->view('footer');
      }
    

    【讨论】:

      【解决方案2】:

      检查文件是否已上传。

      if (!empty($_FILES['image']['name'])) 
       {
          if (!$this->upload->do_upload('image')) 
          {
              // Name isn't empty so a file must have been selected
              $data['error'] = $this->upload->display_errors();
              $this->load->view('header');
              $this->load->view('book_hostel', $data);
              $this->load->view('footer');
          }
          else
          {
               $image_data = $this->upload->data();
          }
      }else
      {
         // No file selected - set default image
         $image_data['file_name'] = "noimage.png"
      }
      
      $data = array(
                      'image_name' => $image_data['file_name'],
                      'student_id' => $this->input->post('student_id'),
                      'student_name' => $this->input->post('student_name'),
                      'father_name' => $this->input->post('father_name'),
                      'program_name' => $this->input->post('program_name'),
                      'mobile' => $this->input->post('mobile'),
                      'email' => $this->input->post('email'),
                      'hostel_name' => $this->input->post('hostel_name')
      
                  );
      $this->student_model->form_insert($data);
      //$this->sendEmail();
      $data['message'] = 'Data Inserted Successfully';
      redirect('hostel');
      

      这可以进一步重构,但重点是您可以检查$_FILES['field_name']['name'] 以查看是否选择了文件。

      【讨论】:

      • 不,我没有收到任何错误,但没有设置任何默认图像
      • var_dump($image_data['file_name']);超出if条件
      猜你喜欢
      • 2018-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-18
      • 2013-09-03
      • 1970-01-01
      • 2020-03-04
      • 2017-08-16
      相关资源
      最近更新 更多