';allowed_types';但是可以上载任何格式


'allowed_types' are set but any format can be uploaded

这是我的代码:

config['upload_path'] = './media/resumes/';
            $config['allowed_types'] = 'pdf|doc|docx';
            $config['max_size'] = '100000';
        $this->load->library('upload', $config);
        $this->upload->do_upload("myfile");           
    //    enabling this code will result in duplication of uploaded file
        $file = $this->upload->data();
            $filename=$file['file_name'];

问题是,我尝试了.jpg甚至.php,文件都成功上传了!没有错误!

为什么会这样?我该如何解决?

代码中没有任何错误,添加此行$this->upload->display_errors();并检查其工作

$config['upload_path'] = './media/resumes/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
$this->upload->do_upload("myfile");
$this->upload->display_errors(); //add this line too
$file = $this->upload->data();
$filename = $file['file_name'];
$this->load->library('upload', $config);

之后尝试添加

$this->upload->initialize($config);

也许对你有帮助。

$config['upload_path'] = './media/resumes/';
$config['allowed_types'] = 'pdf|doc|docx';
$config['max_size'] = '100000';
$this->load->library('upload', $config);
if(!$this->upload->do_upload("myfile")){
$this->upload->display_errors();
}
else{
$file = $this->upload->data();
$filename = $file['file_name'];
}

试试这个条件,它应该有效。。

public function makeUpload($field, $place) {
            $config['upload_path']          = './uploads/' . $place;
            $config['allowed_types']        = 'png';
            $config['remove_spaces']        = TRUE;
            $config['encrypt_name']         = TRUE;
            $config['max_size']             = 30003072;
            $this->load->library('upload', $config);
            $this->upload->do_upload($field);
            $errors = $this->upload->display_errors();
            if ($errors) {
                $this->error(array($errors));
            } else {
                $this->success(array($this->upload->data()));
            }
}
public function success($data){
    header('Content-Type: application/json');
    http_response_code(200);
    die(json_encode($data));
}
public function error($data){
    http_response_code(404);
    header('Content-Type: application/json');
    die(json_encode($data));
}

不要忘记在html输入中使用进行验证

accept="接受";图像/*";或者accept="否";application/pdf";此处的更多类型