codeigniter 已设置'allowed_types',但可以上载任何格式

ttisahbt  于 2022-12-07  发布在  其他
关注(0)|答案(4)|浏览(88)

这是我的代码:

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,文件都成功上传了!没有错误!
为什么会这样?我该如何解决?

xxslljrj

xxslljrj1#

代码中没有任何错误,添加此行$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'];
gopyfrb3

gopyfrb32#

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

在此之后尝试添加

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

也许这对你有帮助。

yhuiod9q

yhuiod9q3#

$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'];
}

试试这个条件,它应该工作。

blpfk2vs

blpfk2vs4#

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输入中的验证,使用
接受=“image/*”或接受=“application/pdf”here中的更多类型

相关问题