2016-10-06 57 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);