使用不在codeigniter中工作的图像上传数据

时间:2016-01-29 03:37:16

标签: database codeigniter upload

我目前正在创建一个模块,用户可以将带有一些细节的图像上传到数据库,但是即使验证显示我还没有工作,我已经创建了图像的上传目录

控制器:

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

        $config['overwrite'] = TRUE;
        $this->load->library('upload', $config);
        $this->form_validation->set_rules('type', 'Type', 'required');
        $this->form_validation->set_rules('school', 'School', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|valid_email');
        if (!$this->upload->do_upload() || !$this->form_validation->run()) {
            $error = array('error' => $this->upload->display_errors());
            redirect('user/payment');
        } else {
            $data = $this->upload->data();
            $this->thumb($data);
            $file = array(
                'img_name' => $data['raw_name'],
                'thumb_name' => $data['raw_name'] . '_thumb',
                'ext' => $data['file_ext'],
                'type' => $this->input->post('type'),
                'school' => $this->input->post('school'),
                'email' => $this->input->post('email'),

            );

            $data = array('upload_data' => $this->upload->data());
            $this->user_model->add_image($file);

               // redirect('user/home_register');
                $this->load->view('user/upload_success');
        }
    }
    public function thumb($data) {
        $config['image_library'] = 'gd2';
        $config['source_image'] = $data['full_path'];
        $config['create_thumb'] = TRUE;
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 275;
        $config['height'] = 250;
        $config['new_image'] = './thumbs/';
        $this->load->library('image_lib', $config);
        $this->image_lib->resize();
    }

查看:

<!DOCTYPE html>
  <html lang="en">
      <head>
          <meta charset="utf-8">
          <meta http-equiv="X-UA-Compatible" content="IE=edge">
          <meta name="viewport" content="width=device-width, initial-scale=1">
          <meta name="description" content="">
          <meta name="author" content="">
          <title>Technofest - user</title> 
           <?php echo link_tag('css/bootstrap.min.css'); ?>
           <?php echo link_tag('jumbotron-narrow.css'); ?>

      </head>

  <body>
    <br>
    <div align="center">

  </div>
    <br>
    <div class ="container">



     <ul class="nav nav-pills nav-justified">

          <li role="presentation"><a href="<?php echo base_url() . 'user/index';?>">Home</a></li>
          <li role="presentation" ><a href="<?php echo base_url() . 'user/about';?>">About</a></li>
          <li role="presentation"><a href="<?php echo base_url() . 'user/contact';?>">Contact</a></li>
          <li role="presentation" class="active" ><a href="<?php echo base_url() . 'user/home_register';?>">Register</a></li>
        </ul>
    </div>

<br>
    <br>
<div>
        <?php echo form_open_multipart('user/do_upload'); ?>
          <?php if (validation_errors()): ?>
           <div class="alert alert-danger alert-dismissible" role="alert">
           <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>                            
           <?php echo validation_errors(); ?>
         </div>
            <?php endif ?>         


 <div class ="container center-block">

  <div class="jumbotron">
    <div class ="panel panel-primary">
        <div class="panel-heading">
          <h3 class="panel-title">Payment</h3>
        </div>

        <div class="panel-body">

        <div class="form-group">
        <div class="row">
                    <label class="col-sm-2 control-label">Type:</label>


          <input type="radio" id="indiv" name="regtype" value="<?php echo set_value('type'); ?>" id='1'<?php echo set_radio('type','Individual', TRUE)?>/> Invidivual
          <input id="bat" type="radio" name="regtype"  value="<?php echo set_value('type'); ?>" id='2' <?php echo set_radio('type','Batch')?>/> Batch
                 <?php echo form_error('type'); ?> 
                    <br><br>
          <label class="col-sm-2 control-label">Payment for:</label>
        </div>
      </div>



         <div class="form-group">
        <div class="row">
            <label class="col-sm-2 control-label">School:</label>
            <div class="col-sm-5">
              <input type="text" class="form-control" placeholder="Enter School Name" name="school" value="<?php echo set_value('school'); ?>">
            </div>
            </div>
          </div>
          <?php echo form_error('school'); ?> 


       <div class="form-group">
        <div class="row">
            <label class="col-sm-2 control-label">Email:</label>
            <div class="col-sm-5">
              <input type="text" class="form-control" placeholder="Enter Email" name="Email" value="<?php echo set_value('email'); ?>">
            </div>
            </div>
          </div>
          <?php echo form_error('email'); ?> 
          <input type="file" id="imgInp" name = "pic" required width = "10px">


           <div class="col-sm-offset-2 col-sm-10">
             <button type="button, submit"  class="btn btn-primary " style="border-radius: 0;">
                             Upload
                            </button>


              <a href="<?php echo base_url().'user/home_register';?>" type="button" class="btn btn-primary">Back</a>

           </div>
        <?php echo form_close() ?>

        </div>
      </div>


</div>


    <script src="../../assets/js/ie10-viewport-bug-workaround.js"></script>
  </body>
  </html>

模型

function add_image($data) {
        $this->db->set('payment_date', 'NOW()', FALSE); 
        $this->db->insert('payment', $data);
    } 

我在db中有8个字段 id(自动增量),payment_data,类型,学校,电子邮件,img_name,thumb_name和ext

2 个答案:

答案 0 :(得分:1)

尝试提供name = "userfile"

只需更换

即可
<input type="file" id="imgInp" name = "pic" required width = "10px"> 

<input type="file" id="imgInp" name = "userfile" required width = "10px">

答案 1 :(得分:1)

尝试将图片字段名称添加到

In [69]: A
Out[69]: 
array([[ 2,  3, -6, -6, -4],
       [-5, -6, -1, -1,  3],
       [-8,  5, -7, -9, -9],
       [-3,  0,  7, -5, -6]])

In [70]: np.nanmean(np.where(A>=0,A,np.nan),axis=1)
Out[70]: array([ 2.5,  3. ,  5. ,  3.5])

$this->upload->do_upload('pic') 更改为Raw Name