2017-07-31 114 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错误日志? –