Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 图像未上载到codeigniter中的数据库中_Php_Image_Codeigniter_Uploading - Fatal编程技术网

Php 图像未上载到codeigniter中的数据库中

Php 图像未上载到codeigniter中的数据库中,php,image,codeigniter,uploading,Php,Image,Codeigniter,Uploading,我在codeigniter中创建了一个小表单。所有字段都正确插入到数据库中,其中作为文件字段(照片)的字段既不会上载也不会插入到数据库中 这是我的模型、视图和控制器文件 ****我的视图文件****(form1.php) Q 申请表 一般资料 *候选人姓名: 我没有看到任何保存文件的代码。您将文件上载到临时文件夹,该文件夹在脚本结束时消失。在开始将任何内容保存到数据库之前,脚本已结束 下面是一个简短的示例代码: if (!empty($_FILES)) { $tempFile = $_

我在codeigniter中创建了一个小表单。所有字段都正确插入到数据库中,其中作为文件字段(照片)的字段既不会上载也不会插入到数据库中

这是我的模型、视图和控制器文件

****我的视图文件****(form1.php)


Q
申请表
一般资料 *候选人姓名:
我没有看到任何保存文件的代码。您将文件上载到临时文件夹,该文件夹在脚本结束时消失。在开始将任何内容保存到数据库之前,脚本已结束

下面是一个简短的示例代码:

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder . $_FILES['Filedata']['name'];
        move_uploaded_file($tempFile,$targetFile);
}
您需要使用move\u上传的\u file()函数来保存它


另一方面,我不会在视图文件中运行查询。你有一个模型。这就是MVC和codeigniter的全部要点。

您需要在do_upload()函数中将输入类型文件控件名作为参数传递。默认情况下,它是“userfile”

<?php
class Form1 extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('form1_model');
    }

    function index()
    {   
        $this->load->helper(array('form', 'url'));
         //$this->load->helper('form');
        $this->load->library('form_validation');
        $this->load->view('templates/header');
        //$this->load->helper(array('form1', 'url'));


        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        $this->load->database();

        $this->form_validation->set_rules('candidate_name', 'Candidate name', 'required');
        //$this->form_validation->set_rules('username', 'Username', 'required|callback_username_check');
        $this->form_validation->set_rules('father_name', 'Father name', 'required');
        $this->form_validation->set_rules('mother_name', 'Mother name', 'required');
        $this->form_validation->set_rules('gender', 'Gender', 'required');
        $this->form_validation->set_rules('category', 'Category', 'required');
        $this->form_validation->set_rules('date5', 'Date of birth', 'required|callback_dob_check');
        //$this->form_validation->set_rules('photo', 'Photo', 'required|callback_photo_check');
        $this->form_validation->set_rules('mycheck', 'Newsletter', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[school_registration.email]');
        $this->form_validation->set_rules('mailing_address', 'Mailing address', 'required');
        $this->form_validation->set_rules('city', 'City', 'required');
        $this->form_validation->set_rules('state', 'State', 'required');
        $this->form_validation->set_rules('country', 'Country', 'required|callback_country_check');
        $this->form_validation->set_rules('photo', 'Photo', 'callback_do_upload');




        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('form1');
        }
        else
        {   
            $this->form1_model->insert_records();
            $this->load->view('formsuccess');
        }

        $this->load->view('templates/footer');
    }

    public function dob_check($str)
    {
       if($str == '0000-00-00')
       {
          $this->form_validation->set_message('dob_check', 'Please select your date  of birth');
          return FALSE;
       }
    }

   public function photo_check($str)
    {
      if (empty($_FILES['photo']['name']))
        {
           $this->form_validation->set_rules('photo', 'Photo', 'required');
        }
    }




    public function country_check($str)
    {
       if($str == '0')
       {
          $this->form_validation->set_message('country_check', 'Please select your country');
          return FALSE;
       }
    }



    public function username_check($str)
    {
        if ($str == 'test')
        {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }


    function do_upload()
    {
        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width']  = '1024';
        $config['max_height']  = '768';

        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload())
        {
            $error = array('error' => $this->upload->display_errors());

            //$this->load->view('upload_form', $error);
            $this->form_validation->set_message('do_upload', $error);
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            $this->load->view('upload_success', $data);
        }
    }

}
?>



----------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------
<?php
class Form1_model extends CI_Model {

    public function __construct()
    {
        $this->load->database();
    }

    public function insert_records()
   {

//      $upload_data = $this->upload->data();
//      print_r($upload_data);
//      //echo $upload_data['photo'];
        //print_r($this->input->post('mycheck'));
        $temp=$this->upload->data();
        var_dump($temp);
        $image=$temp['file_name'];// to get image file name rom upload script , as it could be stored in the databae

        $hobbies_implode=implode(",",$this->input->post('mycheck'));
        $data = array(
        'candidate_name' => $this->input->post('candidate_name'),
        'father_name' => $this->input->post('father_name'),
        'mother_name' => $this->input->post('mother_name'),
        'local_guardian' => $this->input->post('local_guardian'),
        'gender' => $this->input->post('gender'),
        'category' => $this->input->post('category'),
        'dob' => $this->input->post('date5'),
        'email' => $this->input->post('email'),
        'phone_no' => $this->input->post('phone_no'),
        'mobile_no' => $this->input->post('mobile_no'),
        'mailing_address' => $this->input->post('mailing_address'),
        'city' => $this->input->post('city'),
        'state' => $this->input->post('state'),
        'photo' => $image,
        'hobbies' => $hobbies_implode,
        'country' => $this->input->post('country')
        );
        return $this->db->insert('school_registration', $data);
   }
}
if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder . $_FILES['Filedata']['name'];
        move_uploaded_file($tempFile,$targetFile);
}