我有一个问题,在代码点燃上传图像

时间:2015-05-07 12:13:32

标签: php html5 codeigniter

我的第一个问题是将我的图片上传到我的路径目录中,另一个是将其保存在我的数据库中

所以这是我的控制器" dash.php"

function add(){
    $this->form_validation->set_rules('book_title', 'trim|required|xss_clean');
    $config['upload_path'] = './assets/img/';
    $config['allowed_types'] = 'jpg|png|gif';
    $this->load->library('upload', $config);

    if(!$this->form_validation->run() && !$this->upload->do_upload()){
        //load dash
        $this->load->model('Books_model_admin');
        $results = $this->Books_model_admin->search('', '');
        $data['num_req'] = $results['num_request'];

        $this->load->model('Reserve_display_model');
        $results = $this->Reserve_display_model->active('', '');
        $data['num_request1'] = $results['num_request1'];

        //display number of mess
        $this->load->model('Message_model');
        $results = $this->Message_model->get_list_mess('', '');
        $data['mess_results'] = $results['mess_row'];

        $this->load->view('content/admin_header', $data);
        $this->load->view('content/admin_add');
        $this->load->view('content/admin_footer');
    } else {
        $data->image_name = $this->input->post('image_name');
        $data->book_title = $this->input->post('book_title');
        $data->book_author = $this->input->post('book_author');
        $data->book_status = $this->input->post('book_status');
        $data->book_publisher = $this->input->post('book_publisher');
        $data->book_date_published = $this->input->post('book_date_published');
        $data->book_issue = $this->input->post('book_issue');
        $data->book_isbn = $this->input->post('book_isbn');
        $file_data = $this->upload->data('file_name');
        $data->image = base_url().'assets/img/'.$file_data['file_'];

        if($this->crud_model->create_data($data)){
            $this->session->set_flashdata('list_created', 'The book has been added successfully');
            //redirect to dash with error above
            redirect('dash');
        }
    }
}

这是我的模型" crud_model.php"

function create_data($data){
    $insert = $this->db->insert('book_list', $data);
    return $insert;
}

这是我的观点" admin_add"

    <div class="col-md-10 col-xs-10 col-sm-10 col-lg-10" id="content1">
      <div class="row"><!--content row-->
        <div id="content">
          <div class="row"><!--row-->

          <div class='col-md-2 col-xs-2 col-sm-2 col-lg-2'></div>
          <div class="col-md-8 col-xs-8 col-sm-8 col-lg-8"><!--col-md-8 col-xs-8 col-sm-8 col-lg-8-->
          <?php $attributes = array('id' => 'login_form', 'class' => 'form-horizontal'); ?>
              <!--display errors-->

              <!--Field: First Name-->
              <div class="form-group">
                <div class="col-xs-12">
                 <h1><center>ADD Book</center></h1>
                </div>
              </div>
              <?php echo form_open_multipart('dash/add', $attributes); ?>
              <!--Field: First Name-->
                <div class="form-group">
                  <div class="col-xs-12">
                    <?php echo validation_errors('<p class="bg-danger text-danger">'); ?>
                  </div>
                </div>
                <!--Field: Upload image-->
                <div class="form-group">
                  <div class="col-md-6">
                      <?php
                        $data = array('name' => 'image_name',
                              'placeholder' => 'Image Name',
                              'class' => 'form-control',
                              'value' => set_value('image_name'));
                      ?>
                  <?php echo form_input($data); ?>
                  </div>
                  <div class="col-md-6">
                  <?php echo form_upload('file_name'); ?>
                  </div>
                </div>
              <!--Field: First Name-->
                <div class="form-group">
                  <div class="col-md-6">
                      <?php
                        $data = array('name' => 'book_title',
                              'placeholder' => 'Title',
                              'class' => 'form-control',
                              'value' => set_value('book_title'));
                      ?>
                  <?php echo form_input($data); ?>
                  </div>
                  <div class="col-sm-6">
                  <?php
                    $data = array('name' => 'book_author',
                          'placeholder' => 'Author',
                          'class' => 'form-control',
                          'value' => set_value('book_author'));
                  ?>
                  <?php echo form_input($data); ?>
                  </div>
                </div>
              <!--Field: Email Address-->
                <div class="form-group">
                  <div class="col-sm-12">
                <?php
                  $data = array('name' => 'book_status',
                          'placeholder' => 'Book Status',
                          'class' => 'form-control',
                          'value' => set_value('book_status'));
                ?>
                <?php echo form_input($data); ?>
                  </div>
                </div>
              <!--Field: Username-->
                <div class="form-group">
                  <div class="col-sm-12">
                <?php
                  $data = array('name' => 'book_publisher',
                          'placeholder' => 'Publisher',
                          'class' => 'form-control',
                          'value' => set_value('book_publisher'));
                ?>
                <?php echo form_input($data); ?>
                  </div>
                </div>
              <!--Field: Password-->
                <div class="form-group">
                  <div class="col-sm-12">
                <?php
                  $data = array('name' => 'book_date_published',
                          'placeholder' => 'Date Published',
                          'class' => 'form-control',
                          'type' => 'date',
                          'value' => set_value('book_date_published'));
                ?>
                <?php echo form_input($data); ?>
                  </div>
                </div>
              <!--Field: Password2-->
                <div class="form-group">
                  <div class="col-sm-12">
                <?php
                  $data = array('name' => 'book_issue',
                          'placeholder' => 'Issue',
                          'class' => 'form-control',
                          'value' => set_value('book_issue'));
                ?>
                <?php echo form_input($data); ?>
                  </div>
                </div>
                <!--Field: Password2-->
                <div class="form-group">
                  <div class="col-sm-12">
                <?php
                  $data = array('name' => 'book_isbn',
                          'placeholder' => 'ISBN#',
                          'class' => 'form-control',
                          'value' => set_value('book_isbn'));
                ?>
                <?php echo form_input($data); ?>
                  </div>
                </div>
              <!--submit button-->
                <div class="form-group">
                  <div class="col-sm-3">
                    <?php
                  $data = array('name' => 'submit',
                          'class' => 'btn btn-success',
                          'value' => 'Add Book');
                ?>
                <?php echo form_submit($data); ?>
                  </div>
                    <div class="col-sm-3">
                      <a href="<?php echo site_url();?>dash" type="button" class="btn btn-warning">Cancel</a>
                    </div>
                  </div>
              <?php echo form_close(); ?>
              </div><!--col-md-8 col-xs-8 col-sm-8 col-lg-8-->
              <div class='col-md-2 col-xs-2 col-sm-2 col-lg-2'></div>

              </div><!--row-->
        </div>
      </div><!--content row-->
    </div>
  </div><!--row-->

</div><!--content-->

在数据库中保存带有路径目录的图像名称,而不是保存我得到的图像名称&#34; array&#34;像这样http://localhost/system/assets/img/array,我应该得到的是http://localhost/system/assets/img/imagename.jpg

另一个是我无法将其保存在我拥有的路径目录中。

1 个答案:

答案 0 :(得分:0)

  1. 确保您在视图页面中使用<form>属性enctype="multipart/form-data"

  2. 确保您的上传路径有效且base_url()已正确定义。

  3. 从我的观点来看,上述控制器代码没有错误。

    检查并告诉我