CodeIgniter - AJAX上传错误:保存文件时出错,请重试

时间:2015-02-26 01:21:02

标签: php ajax codeigniter

上传文件后,我收到以下返回错误:

{" status":"错误"," msg":"保存文件时出错,请再试一次。&#34 ;}

这是我的服务器端代码:

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));
    }

提前致谢!

1 个答案:

答案 0 :(得分:0)

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

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

如果这不起作用,则问题在于您的模型方法。您需要向我们提供该代码,以便我们继续检查它。