如果用户未在codeigniter中定义,如何设置默认图像

时间:2016-02-09 04:31:35

标签: php image codeigniter upload

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

<?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');

         }
    }
}

2 个答案:

答案 0 :(得分:0)

  

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

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

答案 1 :(得分:0)

检查文件是否已上传。

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']以查看是否选择了文件。