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

Php 如果用户未在codeigniter中定义,如何在中设置默认图像,php,image,codeigniter,upload,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->

我的控制器是,如果用户在表单中输入其详细信息时未上载任何图像,我如何在数据库中插入默认图像名称

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

         }
    }
}

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


检查文件是否已上载

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

这可以进一步重构,但关键是您可以检查
$\u FILES['field\u name']['name']
以查看是否选择了文件。

否我没有收到任何错误,但没有设置任何默认的imagevar\u转储($image\u data['file\u name']);超出if条件
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');