我正在尝试使用代码点火器框架使用 Jquery 和 ajax 函数上传图像.我可以看到错误吹:


I'm trying to upload image with Jquery and ajax function using codeigniter framework. i can see the error blow:

遇到 PHP 错误

严重性: 通知消息:

未定义的索引:上传图像

文件名:型号/upload_model.php

行号:26

我的代码:

控制器

public function do_upload(){
    $this->upload->do_upload();
    if($this->upload_model->Doupload()){
        echo 1;
    }else{
        echo 0;
    }
}
 private function set_config_option(){
    $config =array(
        'allowed_type' => 'gif|jpg|png|jpeg|pdf|doc|xml|zip|rar',
        'file_size'  => '100',
        'max_width'  => '1024',
        'overwrite'  => TRUE,
        'max_height' => '768',
    );
    return $config;
}

 private function set_config_option(){
    $config =array(
        'allowed_type' => 'gif|jpg|png|jpeg|pdf|doc|xml|zip|rar',
        'file_size'  => '2048000',
        'max_width'  => '1024',
        'overwrite'  => TRUE,
        'max_height' => '768'
    );
    return $config;
}
public function Doupload(){         
    $target_path = 'uploads/';
    $target_file = $target_path. basename($_FILES['uploadimg']['name']);
    $base_url = base_url();
    $img_title = $this->input->post('imgname');
    $this->upload->initialize('upload', $this->set_config_option());
  //  $img = $this->upload->do_upload();    
   }

请帮帮我...

模型未收到 $_FILES 对象。为什么将上传过程分成 2 个文件(模型和控制器?,你在数据库中存储一些东西?)。在代码启动器文档中:https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html 都在同一个文件(控制器)中。

尝试

按照示例操作或尝试添加控制器:

$this->upload_model->Doupload($_FILES);

在模型中:

public function Doupload($file){
    $target_path = 'uploads/';
    $target_file = $target_path. basename($file['uploadimg']['name']);

我猜你用: $this->load->library('upload'); 或通过自动加载配置类加载库。 希望对您有所帮助!

将其添加到您的 ajax 请求中,如果这有助于:)我假设你使用表单/多部分

 xhr: function() {
        var myXhr = $.ajaxSettings.xhr();
        return myXhr;
},
相关文章: