CodeIgniter - AJAX Upload error: Something went wrong when saving the file, please try again

CodeIgniter - AJAX Upload error: Something went wrong when saving the file, please try again

上传文件时,出现以下 return 错误:

{"status":"error","msg":"Something went wrong when saving the file, please try again."}

这是我的服务器端代码:

public function upload_file(){
        $status = "";
        $msg = "";
        $file_element_name = 'files';

        if ($status != "error")
        {
            $base_url = base_url();
            $config['upload_path'] = './files/';
            $config['allowed_types'] = '*';
            $this->load->library('upload');
            $this->upload->initialize($config);
            if (!$this->upload->do_upload($file_element_name))
            {
                $status = 'error';
                $msg = $this->upload->display_errors('', '');
            }
            else
            {
                $data = $this->upload->data();
                $file_id = $this->Rate_model->insert_file($data['file_name']);
                if($file_id)
                {
                    $status = "success";
                    $msg = "File successfully uploaded";
                }
                else
                {
                    unlink($data['full_path']);
                    $status = "error";
                    $msg = "Something went wrong when saving the file, please try again.";
                }
            }
            @unlink($_FILES[$file_element_name]);
        }
        echo json_encode(array('status' => $status, 'msg' => $msg));
    }

提前致谢!

为了使用您的模型,您首先需要加载它,进行以下更改:

 $this->load->model('Rate_model');
 $file_id = $this->Rate_model->insert_file($data['file_name']);

如果这不起作用,那么问题就出在你的模型方法上。您需要向我们提供该代码,以便我们可以继续检查它。