【问题标题】:Uploading a file in codeigniter在 codeigniter 中上传文件
【发布时间】:2017-11-27 22:08:42
【问题描述】:

我在尝试上传文件时收到一条错误消息,指出 You did not select a file to upload.,尽管我已选择要上传的文件。我正在使用一个使用引导程序样式的输入文件。有问题吗?我按照 Codeigniter 用户指南中的说明进行文件上传。谁能帮我找出我的代码中的错误?

谢谢

控制器

public function create()
{
    $config['upload_path']          = './uploads/';
    $config['allowed_types']        = 'gif|jpg|png';

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

    if ( ! $this->upload->do_upload('userfile'))
    {
            echo "Upload failed";
    }
    else
    {
            $data = array('upload_data' => $this->upload->data());

    }

    $data = array(
        'category' => $this->input->post('category');
        'name' => $this->input->post('recipename'),
        'ingredients' => $this->input->post('ingredients'),
        'directions' => $this->input->post('directions'),
        'date' => date("Y/m/d")

    );

    $this->model->insert($data);
    redirect('', 'refresh');
}

查看

<form action="<?php echo base_url();?>home/create" method="POST" role="form" multipart>
        <legend>New Recipe</legend>

        <div class="form-group">
            <label for="">Category</label>

            <select name="category" id="input" class="form-control">
                <?php foreach ($category as $value) {?>
                <option value="<?php echo $value->category; ?>"><?php echo $value->category;?></option>
                <?php } ?>

            </select>


            <label>Name</label>
            <input type="text" name="recipename" class="form-control" id="" placeholder="Recipe Name">
            <label>Image</label>
            <div class="input-group">
                <label class="input-group-btn">
                    <span class="btn btn-primary">
                        Browse&hellip; <input type="file" name="userfile" style="display: none;" multiple>
                    </span>
                </label>
                <input type="text" class="form-control" readonly>
            </div>
            <label>Ingredients</label>
            <textarea name="ingredients" id="input" class="form-control" required="required"></textarea>
            <label>Directions</label>
            <textarea name="directions" id="input" class="form-control" required="required"></textarea>

        </div>



        <button type="submit" class="btn btn-primary">Submit</button>
      </form>

 <script type="text/javascript">


    $(function() {

      // We can attach the `fileselect` event to all file inputs on the page
      $(document).on('change', ':file', function() {
        var input = $(this),
            numFiles = input.get(0).files ? input.get(0).files.length : 1,
            label = input.val().replace(/\\/g, '/').replace(/.*\//, '');
        input.trigger('fileselect', [numFiles, label]);
      });

      // We can watch for our custom `fileselect` event like this
      $(document).ready( function() {
          $(':file').on('fileselect', function(event, numFiles, label) {

              var input = $(this).parents('.input-group').find(':text'),
                  log = numFiles > 1 ? numFiles + ' files selected' : label;

              if( input.length ) {
                  input.val(log);
              } else {
                  if( log ) alert(log);
              }

          });
      });

    });
</script>

【问题讨论】:

  • 你是通过ajax上传文件吗?
  • 不,我不是。我只是在使用 php。
  • @Beldion 将此适当的表单属性enctype="multipart/form-data" 添加到视图。此外,输入文件元素选择了多个文件,但在您的控制器中将不支持多个文件上传检查此以帮助您link

标签: php codeigniter upload


【解决方案1】:

在你的 html 表单标签中使用 enctype。

<form action="<?php echo base_url();?>home/create" method="POST" role="form" enctype="multipart/form-data" >

现在试试。它会工作的。:)

【讨论】:

  • 它现在显示一个不同的错误,上面写着The upload path does not appear to be valid
  • 现在您的文件已到达控制器,但它无法找到您的目录来存储它。检查stackoverflow.com/questions/6159851/…
猜你喜欢
  • 1970-01-01
  • 2016-03-11
  • 2019-01-22
  • 2013-03-11
  • 2013-02-12
  • 2020-02-05
  • 1970-01-01
  • 2011-10-19
  • 2011-11-06
相关资源
最近更新 更多