使用CodeIgniter上传文件

时间:2013-06-12 21:15:38

标签: php codeigniter image-uploading

我正在尝试使用CodeIgniter构建一个具有图像上传器并且似乎被卡住的表单。当我注释掉文件上传时数据库提交工作,但是当我重新放入文件上载器时没有任何反应。当我运行它时,我没有收到任何错误。

我确信我错过了一些明显的东西,但我不能为我的生活找出它是什么。任何帮助将不胜感激!

控制器

public function Upload()
{
    $data['segment'] = 'Contact us';

    // Checks if user is logged in
    if ( $this->session->userdata('logged_in') === TRUE) {
        $data['user'] = $this->session->userdata('username');
    }
    else {
        $data['user'] = null;
    }

    $url = base_url();

            $config['upload_path'] = './uploads/';

            // Load an initialize
    $this->load->helper('form');
    $this->load->library('form_validation');
            $this->upload->initialize($config);

    $this->form_validation->set_rules("name", '"name"', 'required|trim|max_length[30]|xss_clean');

    // Uploads file
    if (! $this->upload->do_upload('userfile')) {
        $data['error'] = array('error' => $this->upload->display_errors());
    }

    else {
        $data = array('upload_data' => $this->upload->data());
        $img_data = $this->upload->data();

        if (FALSE !== $this->form_validation->run()) {
                            // Database submission
            $submission = new ReptileModel();

            $submission->name           = $this->input->post("name");
            $submission->sex            = $this->input->post("sex");
            $submission->hatch_date     = $this->input->post("date");
            $submission->morph          = $this->input->post("morph");
            $submission->weight         = $this->input->post("weight");
            $submission->price          = $this->input->post("price");
            $submission->reptile_id     = $this->input->post("rid");
            $submission->description    = $this->input->post("description");
            $submission->image      = $img_data['file_name'];

            if ($submission->save()) {

                redirect('main/index');

            }
            else {
                $this->session->set_flashdata('message','Not uploaded');
            }
        }
    }
    $this->upload->display_errors();
    $this->load->view('header', $data);
    if ( $this->session->userdata('logged_in') === TRUE) {
        $this->load->view('admin/reptileUpload', $data);
    }
    else {
        redirect('main/index');
    }
    $this->load->view('footer');

查看页面

<?php
$rname_field = array(
  'name' => 'name',
  'id' => 'name',
  'value' => set_value('name')
);

$rsex_field = array(
  'name' => 'sex',
  'id' => 'sex',
  'value' => set_value('sex'),
  'placeholder' => 'Male or female'
);

$rdate_field = array(
  'name' => 'date',
  'id' => 'date',
  'value' => set_value('date'),
  'placeholder' => 'January 1, 2100'
);

$rmorph_field = array(
  'name' => 'morph',
  'id' => 'morph',
  'value' => set_value('morph')
);

$rweight_field = array(
  'name' => 'weight',
  'id' => 'weight',
  'value' => set_value('weight'),
  'placeholder' => 'Please only put a number, no letters.'
);

$rprice_field = array(
  'name' => 'price',
  'id' => 'price',
  'value' => set_value('price'),
  'placeholder' => 'The price with a decimal, but no dollar sign.'
);

$rid_field = array(
  'name' => 'rid',
  'id' => 'rid',
  'value' => set_value('rid')
);

$rdescriptione_field = array(
  'name' => 'description',
  'id' => 'description',
  'value' => set_value('description'),
  'rows' => '5',
  'placeholder' => 'A short descrition.'
);

?>
<?php echo $error;?>
<div class="row">
  <?php print form_open_multipart('admin/upload'); ?>
    <fieldset class="form-wrapper">
        <div class="large-8 columns">
            <div class="row">
              <div class="large-6 columns">
                <div class="form-field">
                  <label for="name">Name:<br /></label>
                  <?php echo form_input($rname_field); ?>
                </div>
              </div>

              <div class="large-6 columns">
                <div class="form-field">
                  <label for="sex">Sex:<br /></label>
                  <?php echo form_input($rsex_field); ?>
                </div>
              </div>
            </div>

            <div class="row">
              <div class="large-6 columns">
                <div class="form-field">
                  <label for="date">Hatch Date:<br /></label>
                  <?php echo form_input($rdate_field); ?>
                </div>
              </div>

              <div class="large-6 columns">
                <div class="form-field">
                  <label for="morph">Morph:<br /></label>
                  <?php echo form_input($rmorph_field); ?>
                </div>
              </div>
            </div>

            <div class="row">
              <div class="large-4 columns">
                <div class="form-field">
                  <label for="weight">Weight:<br /></label>
                  <?php echo form_input($rweight_field); ?>
                </div>
              </div>

              <div class="large-4 columns">
                <div class="form-field">
                  <label for="price">Price:<br /></label>
                  <?php echo form_input($rprice_field); ?>
                </div>
              </div>

              <div class="large-4 columns">
                <div class="form-field">
                  <label for="rid">ID:<br /></label>
                  <?php echo form_input($rid_field); ?>
                </div>
              </div>
            </div>

            <div class="row">
              <div class="large-12 columns">
                <div class="form-field">
                  <label for="description">Description:<br /></label>
                  <?php echo form_textarea($rdescriptione_field); ?>
                </div>
              </div>
            </div>

            <div class="row">
              <div class="large-12 columns">
                <div class="form-field">
                  <input type="file" name="userfile" id="image" />
                </div>
              </div>
            </div>
        </div>

        <div class="large-4 columns">

            <div class="row">
                <div class="large-12 columns">
                    <div class="form-actions">
                        <input class="button success" type="submit" name="submit" value="Send &raquo;" />
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="large-12 columns">
                    <input class="button alert" type="reset" name="reset" value="Clear" />
                </div>
            </div>

            <div class="row">
                <div class="large-12 columns">
                    <?php print validation_errors('<div data-alert class="alert-box alert">', '<a href="#" class="close">&times;</a></div>'); ?>
                </div>
            </div>
        </div>
      </fieldset>
    </div>
  <?php print form_close();?>
</div>

1 个答案:

答案 0 :(得分:3)

添加$config['allowed_types'] = '*';