2017-07-31 70 views
1

的意見/ image.php到codeigneter所以有麻煩我的代碼不能正常工作,因爲我的新本

<form action="upload1" method="post" enctype="multipart/form-data"> 
    <h3>Image Upload Form</h3> 
    <input type="file" name="pic" tabindex="2" required> 
    <input type="text" name="alt" placeholder="Image Alt Text" tabindex="1" 
    required> 
    <button type="submit" id="img-submit" data-submit="Sending">Submit</button> 
    </form> 

控制器/ upload.php的

function upload1() 

    { 
    $config['upload_path']   = './uploads/'; 
      $config['allowed_types']  = 'gif|jpg|png'; 
      $config['max_size']    = 100; 
      $config['max_width']   = 1024; 
      $config['max_height']   = 768; 
      $data = array(
    'image_url' => $this->input->$_FILES['pic']['name'], 
    'alt' => $this->input->post('alt') 
); 
    $result = $this->upload_m->upload1($data); 
    } 

模型/ upload_m.php

<?php 
class upload_m extends CI_Model { 

function __construct() 
{ 
    parent::__construct(); 
} 
function upload1($data) 
    { 
    $data ['image_url']= $this->upload1($data); 
    $data ['alt']= $this->input->post('alt'); 
     $this->db->insert('image', $data); 
    redirect(base_url() . 'index.php/upload/', 'refresh'); 

    } 
} 
    ?> 

當我點擊提交按鈕upload1函數調用,它給錯誤數組字符串轉換 和在模型 致命錯誤:用盡134217728個字節允許存儲器大小(試圖分配262144個字節)在C:\ XAMPP \ htdocs中\笨\應用\模型\ upload_m.php第12行

任何人都可以檢測和告訴我我在做什麼錯誤?

+0

不確定,但您調用$ data ['image_url'] = $ this-> upload1($ data);在upload_m中,但upload1()方法沒有返回任何內容,所以你基本上空白了那個值,或者把它變成一個bool – delboy1978uk

回答

1

你應該改變你的控制器如下:

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

    $data = array(
     'image_url' => $_FILES['pic']['name'], 
     'alt' => $this->input->post('alt') 
    ); 

    if (! $this->upload->do_upload('pic')) { 
     //$error = array('error' => $this->upload->display_errors()); 
     // use $error by passing to a view. Please see the documentation. 
    } else { 
     $result = $this->upload_m->upload1($data); 
     redirect(base_url() . 'index.php/upload/', 'refresh'); 
    } 
} 

和模型:

class upload_m extends CI_Model 
{ 

    function __construct() 
    { 
     parent::__construct(); 
    } 
    function upload1($data) 
    { 
     $image_data = $this->upload->data(); 
     $data['image_url'] = $image_data['file_name']; 
     $data['alt']  = $this->input->post('alt'); 
     return $this->db->insert('image', $data);   
    } 
} 

注意:您不應該在模型內部重定向。

'image_url' => $this->input->$_FILES['pic']['name'], 

上面的行在語法上是錯誤的。並在下面的模型中進行遞歸調用

$data ['image_url']= $this->upload1($data); 
+0

使用您的代碼沒有顯示錯誤,也沒有保存圖像數據庫 –

+0

你可以添加下面的代碼,如果條件下if(!$ this-> upload-> do_upload('pic')){' 'var_dump($ error);' – Niroshan

+0

it NULL NULL –

1

試試這個

在控制器

function upload1() 
{ 
    $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('pic')) 
    { 
      $error = array('error' => $this->upload->display_errors()); 
      $this->load->view('ViewName', $error); 
    } 
    else 
    { 
      $data = array('upload_data' => $this->upload->data()); 
      $alt = $this->input->post('alt'); 

      $this->upload_m->upload1($data, $alt); 

      $this->load->view('successView', $data); 
    } 

} 

在型號

function upload1($data, $alt) 
{ 
    $data['upload_path']= $this->upload1($data); 
    $data['alt']= $alt; 

    $this->db->insert('image', $data); 
} 

鏈接

  1. CodeIgniter: Allowed memory exhausted
  2. File Uploading Class
+0

我使用你的代碼,但是它沒有顯示任何錯誤,也沒有上傳圖像 –

+0

檢查php錯誤日誌 –

+0

檢查php錯誤日誌? –

相關問題