繁体   English   中英

使用其他输入字段在 codeigniter 中上传文件

[英]File upload in codeigniter with other input field

我已经研究了用于文件上传的 codeigniter 文档并仅使用一个文件字段实现了文件上传,但现在我还有其他输入字段,所以我使用了以下代码,但存在数据库错误“列'用户文件'不能为空”,请告诉我我错在哪里

看法

<html>
<body>
<head>
<title>VALIDATION</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="/resources/demos/style.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
    $( "#datepicker" ).datepicker();
} );
</script>

<script>
    $(document).ready(function(){
        $("#sub").click(function(){
            alert("Please check all required fields are filled.");
        });
});
</script>

</head>
<form method="POST" action="<?php echo site_url('Dob_control/insert');?>" enctype="multipart/form-data">
<p>NAME:</p>
<?php echo form_error('name'); ?>
<input type="text" name="name" value="<?php echo set_value('name');  ?>" size="50">
<p>EMAIL:</p>
<?php echo form_error('email'); ?>
<input type="email" name="email" value="<?php echo set_value('email');  ?>" size="50">
<p>DATE:</p>
<?php  echo form_error('date');   ?>
<input type="date" id="datepicker" name="date" value="<?php echo set_value('date');  ?>" size="50">
<p>NOTES:</p>
<?php echo form_error('notes');  ?>
<textarea name="notes" rows="4" cols="39"><?php echo set_value('notes');  ?></textarea>
<p>IMAGE:</p>
<input type="file" name="userfile" size="20" />
<br>
<input type="submit" id="sub" name="submit" value="submit">
</form>
</body>
</html>

模型

 <?php
    class Dob_model extends CI_Model {
        function __construct(){
            parent::__construct();
            $this->load->database();
        }

        function insert_record($student){   
            $this->db->insert('validate_table', $student);  // insert data into `validate_table table` 
            if ($this->db->affected_rows() > 0) {
                return true;
            } else {
                return false;
            }
        }
    }
    ?>

控制器

<?php
class Dob_control extends CI_controller {
    function __construct(){
        parent::__construct();
        $this->load->model('Dob_model');
    }

    function index(){
        $this->load->helper(array('form','url'));
        $this->load->view('simple_form');
    }

    public function insert(){
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name','Name','required');
        $this->form_validation->set_rules('email','Email','required');
        $this->form_validation->set_rules('date','Date','required');
        $this->form_validation->set_rules('notes','Notes','required',    array( 'required' => 'Please complete this field'));
        if ($this->form_validation->run() == FALSE)
        {

        $this->load->view('simple_form');
        } else {
            if ($this->input->post('submit') == true){
                $student = array(
                            'name'    => $this->input->post('name'),
                            'email'   => $this->input->post('email'),
                            'date'    => $this->input->post('date'),
                            'notes'   => $this->input->post('notes'),
                            'userfile'=> $this->input->post('userfile'));
                $result   =  $this->Dob_model->insert_record($student);
                if($result==true){
                    echo "inserted";
                }else 
                    echo "Not Inserted";
            } } }

    public function do_upload(){
        $config['upload_path']          = './uploads/';
        $config['allowed_types']        = 'gif|jpg|png';
        $config['max_size']             = 10000;
        $config['max_width']            = 10244;
        $config['max_height']           = 7685;
        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload('userfile')) {
    $error = array('error' =>    $this->Dob_control->display_errors());
            $this->load->view('simple_form', $error);
        } else {
            $data = array('upload_data' => $this->Dob_control->data());

            $this->load->view('upload_success', $data);
        } } }
?>

首先加载库。

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

然后上传你的文件

if ( $this->upload->do_upload('userfile'))
{
      $data = array('upload_data' => $this->upload->data());
      // $data will contain your file information
}

请参考https://codeigniter.com/user_guide/libraries/file_uploading.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM