php - 我正在尝试使用 codeigniter 框架使用 Jquery 和 ajax 函数上传图像。我可以看到错误 :

标签 php jquery codeigniter

A PHP Error was encountered

Severity: Notice Message:

Undefined index: uploadimg

Filename: models/upload_model.php

Line Number: 26

我的代码:

Controller

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 对象。为什么你将上传过程分成两个文件(模型和 Controller ?,你在数据库中存储一些东西?)。在 codeiniter 文档中:https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html全部在同一个文件( Controller )中。

尝试遵循示例或尝试在 Controller 中添加:

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

在模型中:

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

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

关于php - 我正在尝试使用 codeigniter 框架使用 Jquery 和 ajax 函数上传图像。我可以看到错误 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36100822/

相关文章:

javascript - 使用 ajax 和 PHP 根据第一个选择框填充第二个选择框

PHP、正则表达式 - 如何使用 preg_replace 转义\n?

php - API 平台 : Normalizing a Mapped Superclass only includes properties from the superclass, 不是子类

Jquery Logo 动画插入

javascript - 如何通过 jQuery 选择器使用 DOM 元素的 Id 或 Class 的一部分?

javascript - 选中后获取所有表行值

javascript - jQuery - 覆盖事件?

Codeigniter - 使用 force_download 函数下载文件

php - 我无法在 codeigniter 中将数据插入数据库

javascript - setInterval 处理不正确