2016-10-06 114 views
0

这是一个被广泛讨论的话题:如何在上传数据库中保存图片url?上传数据库时存储图片url的问题(CodeIgniter)

我已经阅读了他们,但仍然不明白为什么我的控制器不工作。我跟着CodeIgniters documentation on uploading files创建了一个控制器,将文件上传到所需的目录中,到目前为止这么好。我现在想要将图片网址保存在数据库中上传。我修改了this question的代码,但我有多个我无法解决的错误。

1.

消息:非法串偏移 'FILE_NAME'

文件名:控制器/ upload.php的

行号:36

2.Message:未定义属性:上传:: $ db

文件名:controllers/Upload.php

行号:37

3.Message:上的空调用一个成员函数插入件()

文件名:控制器/ upload.php的

行号:37

这里是我的控制器(我有两个字段的测试表疑惑 - 自动递增主键ID和image_url):

<?php 

class Upload extends CI_Controller { 

     public function __construct() 
     { 
       parent::__construct(); 
       $this->load->helper(array('form', 'url')); 
     } 

     public function index() 
     { 
       $this->load->view('upload_form', array('error' => ' ')); 
     } 

     public function do_upload() 
     { 
       $config['upload_path']   = './uploads/'; 
       $config['allowed_types']  = 'gif|jpg|png'; 
       $config['max_size']    = 100; 
       $config['max_width']   = 1024; 
       $config['max_height']   = 768; 

       $this->load->library('upload', $config); 

       if (! $this->upload->do_upload('userfile')) 
       { 
         $error = array('error' => $this->upload->display_errors()); 

         $this->load->view('upload_form', $error); 
       } 
       else 
       { 
         $data = $this->upload->data(); 
         $file_array = $this->upload->data('file_name'); 
         $image['image_url'] = $file_array['file_name']; 
         $this->db->insert('puzz', $image); 
         $this->load->view('upload_success', $data); 
       } 
     } 
} 
?> 

如果有人能指出我的代码中的错误,将会感激!

回答

1

使用插入

$this->load->database(); 

OR变化构造

public function __construct() 
    { 
      parent::__construct(); 
      $this->load->helper(array('form', 'url')); 
      $this->load->database(); 
} 

OR在变化autoload.php

$autoload['libraries'] = array('database'); 

AND改变该代码块

之前添加此
$data = $this->upload->data(); 
$image['image_url'] = $data['file_name']; 
$this->db->insert('puzz', $image); 
$this->load->view('upload_success', $data);