【问题标题】:Codeigniter File upload is not working after updating to latest version.Codeigniter 文件上传在更新到最新版本后无法正常工作。
【发布时间】:2018-10-14 04:55:57
【问题描述】:

现在将项目文件更新到最新版本的 Codeigniter 后,当我上传任何文件时,它会返回此错误:

您尝试上传的文件类型不允许。

代码如下:

public function test_upload(){
    $config = array(
        'upload_path'   => './assets/upload_test',
        'allowed_types' => 'gif|jpg|png|jpeg|pdf',
        'max_size'      => '2048',
    );
    if($_FILES['file']['name'] != '')
    {
        $image = 'file';
        $upload_data = $this->do_upload($image, $config);
        if($upload_data['condition']=='error')
        {
            echo json_encode(array('condition'=>'error', 'message'=>$upload_data['error'].' (User image)')); exit;
        }else{
            $profile_picture['file'] = $upload_data['upload_data']['file_name'];
        }
    }
    print_r($profile_picture);exit;
}

public function do_upload($image, $config)
{
    $this->load->library('upload');
    $this->upload->initialize($config);
    if ( ! $this->upload->do_upload($image))
    {
        $error = array('condition'=>'error', 'error' => $this->upload->display_errors());
        return $error;
    }
    else
    {
        $data = array('condition'=>'success', 'upload_data' => $this->upload->data());
        return $data;
    }
}

搜索了很多,但没有找到任何好的解决方案。感谢大家的回复。

【问题讨论】:

  • 您遇到了什么错误?
  • @Bergin 错误为:"{"condition":"error","message":"您尝试上传的文件类型不被允许。"}"
  • 如果从该配置中删除 'allowed_types' => 'gif|jpg|png|jpeg|pdf' 是否可以正常工作?
  • @Bergin,不,如果没有允许的类型,它就无法工作。
  • 这听起来很明显,但是您上传的是哪种文件?

标签: codeigniter file-upload version


【解决方案1】:

我从谷歌找到我的答案,但我会花很多时间。 只需更改此行:

'allowed_types' => 'gif|jpg|png|jpeg|pdf'

将此行替换为:

'allowed_types' => '*'

我的问题已经解决了。

【讨论】:

  • 您要上传什么类型的文件? '*' 接受所有类型的文件,如果你给出特定的文件类型,它将只允许那些
  • @Bergin 我尝试上传 PFD 和所有图像格式,但您建议的解决方案对我不起作用。感谢您的回复。
猜你喜欢
  • 2022-08-02
  • 2019-07-04
  • 2015-02-15
  • 1970-01-01
  • 2023-02-10
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 2013-04-10
相关资源
最近更新 更多