CodeIgniter不会加载上传库

时间:2014-12-05 22:01:48

标签: php codeigniter codeigniter-form-helper

我在加载CodeIgniter库时遇到问题,

我已经用CodeIgniter构建了三(3)个网站,所以我认为自己熟悉这个框架。

问题仅在于上传lib。我尝试过多种功能,但仍无效。

我甚至尝试过the CodeIgniter example

在谷歌我找不到任何答案,有没有人知道它可能是什么?

class Admin extends CI_Controller {

public function __construct(){
    parent::__construct();
    $this->load->library('session');
    $this->load->library('v');
    $this->load->model('admin_model');
}

public function upload_a_thing($set = null){
    $this->load->helper(array('form', 'url'));
    if(!$set){
        $this->load->view('admin/upload_a_thing');
    }else{
        $this->load->library('Upload');
        if (!$this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
        } else {

            $file = $this->upload->data();
            //          $product = $this->admin_model->get_products($id);
            $newFilePath = $config['upload_path'].'try.jpg';
            $file['file_name'] = str_replace(" ","_",$file['file_name']);
            rename($config['upload_path'].$file['file_name'], $newFilePath);
        }   
    }
}   

CodeIgniter错误

 Undefined property: Admin::$upload

PHP错误

Fatal error: Call to a member function do_upload() on a non-object 

为Spary编辑

        $config['upload_path'] = './media/imgs/products/';
        $this->load->library('upload',$config);
        if (!$this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
        }

3 个答案:

答案 0 :(得分:1)

如果您的表单如下所示:

<input type="file" class="input" name="logo"  />

然后像这样调用do_upload():

 do_upload('logo');

如果它看起来像这样:

 <input type="file" class="input" name="userfile" />

然后这样称呼:

 do_upload();

答案 1 :(得分:0)

做这样的事情

if (!$this->upload->do_upload('userfile')){
            echo $this->upload->display_errors();
        } 

答案 2 :(得分:0)

您的代码......

$this->load->library('Upload');

As per all examples in the documentationupload应该全部为小写,很可能是库未加载的原因。


此外,如果缺少$config数组,则不要遵循文档中的示例代码:

$config['upload_path'] = './uploads/';   // <- preferences
$this->load->library('upload', $config); // <- load library and set configuration

OR

$this->load->library('upload');         // <- load library

$config['upload_path'] = './uploads/';  // <- preferences
$this->upload->initialize($config);     // <- set configuration

OR

// 'upload' library is "auto-loaded"    // <- load library

$config['upload_path'] = './uploads/';  // <- preferences
$this->upload->initialize($config);     // <- set configuration

从首选项图表中,除了upload_path ...

外,它们似乎都是可选的
  

默认值:无
  
  说明:应放置上传文件夹的路径。该文件夹必须是可写的,路径可以是绝对路径或相对路径。

我不确定如果没有upload_path,CodeIgniter如何知道上传文件的位置,因为没有默认值,而且整个配置数组都丢失了。