php - 在 codeigniter 中上传文件

标签 php codeigniter upload

尽管我已选择要上传的文件,但在尝试上传文件时,我收到一条错误消息,指出您未选择要上传的文件。。我正在使用一个使用 bootstrap 样式的输入文件。问题就在于此吗?我按照 Codeigniter 用户指南中的说明进行文件上传。谁能帮我找出代码中的错误吗?

谢谢

Controller

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>

最佳答案

在 html 表单标记中使用 enctype。

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

现在尝试一下。它会起作用的。:)

关于php - 在 codeigniter 中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44736493/

相关文章:

php - 无法通过套接字连接到本地 MySQL 服务器 '/tmp/mysql.sock' (2) 错误

php - 如何在mysql中选择前三行

php - 在 mysql where in 子句中对数组使用内爆

codeigniter - 模式创建时的 Doctrine + CodeIgniter : after generate entities with namespace, "cannot redeclare class"

php - php codeigniter中的电话号码验证

php - 在codeigniter中将字符串分成两部分

Javascript 函数多种形式

upload - 在Liferay中上传后,文件保存在哪个位置?

python - 使用 Python 请求将图像和元数据上传到公共(public) Amazon S3 存储桶

php - 多个文件上传 Angular + Laravel