【问题标题】:Codeigniter 3 : inputerror : The filetype you are attempting to upload is not allowed.Codeigniter 3:输入错误:不允许您尝试上传的文件类型。
【发布时间】:2019-05-20 23:14:03
【问题描述】:

查看:

<form action="#" enctype="multipart/form-data" id="form" class="form-horizontal">

   <div class="form-group">
                            <label class="control-label col-md-3" id="label-photo">Upload Photo </label>
                            <div class="col-md-9">
                                <input name="photo" type="file">
                                <span class="help-block"></span>
                            </div>
                        </div>

控制器:

private function _do_upload()
    {
      /*  $config['upload_path']          = $_SERVER['DOCUMENT_ROOT'] . '/uploads/';
      // */ 
      $config['upload_path']          = 'uploads/';
       // $config['allowed_types']        = 'gif|jpg|png|jpeg';
            $config['allowed_types']        = 'jpg|gif|png|jpeg|JPG|PNG';
        $config['max_size']             = 5000; //set max size allowed in Kilobyte
        $config['max_width']            = 3000; // set max width image allowed
        $config['max_height']           = 3000; // set max height allowed
        $config['file_name']            = round(microtime(true) * 1000); //just milisecond timestamp fot unique name

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

        if(!$this->upload->do_upload('photo')) //upload and validate
        {
            $data['inputerror'][] = 'photo';
            $data['error_string'][] = 'Upload error: '.$this->upload->display_errors('',''); //show ajax error
            $data['status'] = FALSE;
            echo json_encode($data);
            exit();
        }
        return $this->upload->data('file_name');
    }

当我尝试上传出现此错误的图像时,我找不到任何解决方案。通过更改上传路径和允许的类型来尝试。

【问题讨论】:

标签: php codeigniter-3


【解决方案1】:

好的,你试试这个代码。你可以成功上传图片。

查看文件:uoload.php

<form action="upload_controller" method="post" enctype="multipart/form-data">
  First name: <input type="file" name="photo"><br>
  <input type="submit" value="Submit">
</form>

现在这是我的上传控制器:

public function upload_controller(){
     if (count($_FILES) > 0) {
                if (count($_FILES['photo']) > 0) {
                    if ($_FILES['photo']['name'] != '' && $_FILES['photo']['size'] > 0) {
                        $filename = $_FILES['photo']['name'];
                        $filesize = $_FILES['photo']['size'];
                        $config['upload_path'] = '../admin_zency/data/doctors/'; // Your Upload Path Directory
                        $config['allowed_types'] = '*';
                        $config['max_size'] = $this->config->item('image_size');
                        $config['max_width'] = $this->config->item('user_width');
                        $config['max_height'] = $this->config->item('user_height');
                        $this->upload->initialize($config);
                        $this->load->library('upload', $config);
                        $this->upload->do_upload('photo');
                        $imagedata = $this->upload->data();
                        $imagearror = $this->upload->display_errors();
                    }
                }
            }
}

如果此代码对您有帮助,请给予好评。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-08
    • 2017-08-18
    • 2012-04-06
    • 2012-04-22
    • 2011-11-21
    • 2014-12-30
    • 1970-01-01
    • 2023-04-07
    相关资源
    最近更新 更多