2017-07-15 26 views
0

我上传和存储图像到mysql没有活动记录codeigniter时出错。非法字符串偏移上传和存储图像到MySQL没有活动记录codeigniter

这表明

消息:非法串偏移 'file_ext'

不仅file_ext,但也FILE_SIZE。此问题只发生时我不使用活动记录像下面这段代码:

<?php 
if ($_FILES['userfile']['error'] <> 4) 
{ 
    $nmfile = $this->input->post('name'); 

    $config['upload_path']  = './assets/images/user/'; 
    $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
    $config['max_size']   = '2048'; // 2 MB 
    $config['max_width']  = '2000'; //pixels 
    $config['max_height']  = '2000'; //pixels 
    $config['file_name']  = $nmfile; 

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

    if (!$this->upload->do_upload()) 
    { 
    $this->create(); 
    } 
    else 
    { 
     $userfile = $this->upload->data(); 
     $thumbnail    = $config['file_name']; 

     $config['image_library'] = 'gd2'; 

     $config['source_image'] = './assets/images/user/'.$userfile['file_name'].''; 
     // membuat thumbnail 
     $config['create_thumb'] = TRUE; 
     // rasio resolusi 
     $config['maintain_ratio'] = FALSE; 
     // lebar 
     $config['width']   = 150; 
     // tinggi 
     $config['height']   = 150; 

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

     $id    = $this->input->post('id'); 
     $name    = $this->input->post('name'); 
     $username  = $this->input->post('username'); 
     $psw1    = $this->input->post('psw1'); 
     $psw2    = $this->input->post('psw2'); 
     $usertype  = $this->input->post('usertype'); 
     $userfile  = $nmfile; 
     $userfile_type = $userfile['file_ext']; 
     $userfile_size = $userfile['file_size']; 

     $sql = $this->db->query("INSERT INTO user (id, name, username, psw1, psw2, usertype, userfile, userfile_type, userfile_size) 
          VALUES ('$id', '$name', '$username', password('$psw1'), password('$psw2'), '$usertype', '$userfile', '$userfile_type', '$userfile_size') "); 

     $this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>'); 
    redirect(site_url('auth/user')); 
    } 
} 
?> 

任何帮助会因此appricated,谢谢

+0

什么是'$这个 - >值put->后( '名');'? – Tpojka

回答

0

我相信你能实现你的结果在一个可爱的风格像下面:

if (!empty($_FILES)) { 

    $data = array(
     'id' => $this->input->post('id'), 
     'name' => $this->input->post('name'), 
     'username' => $this->input->post('username'), 
     'psw1' => $this->input->post('psw1'), 
     'psw2' => $this->input->post('psw2'), 
     'usertype' => $this->input->post('usertype') 
); 
    $upload = $this->imageUpload(); 
    if (!$upload) { 
    $this->create(); 
    } else { 
    $data['userfile'] = $upload['upload_data']['file_name']; 
    $data['userfile_type'] = $upload['upload_data']['file_ext']; 
    $data['userfile_size'] = $upload['upload_data']['file_size']; 

    $this->db->insert('user', $data); 
    $this->session->set_flashdata('message', '<div class="alert alert-success alert">Data berhasil dibuat</div>'); 
    redirect(site_url('auth/user')); 
    } 
} 

使用这些功能,上传和调整图片的大小

function imageUpload() { 
    $config = array(
     'upload_path' => "uploads", 
     'allowed_types' => "*", 
     'overwrite' => true, 
    'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb) 
    'max_height' => "3000", 
    'max_width' => "3000" 
); 
    $this->upload->initialize($config); 
    $this->load->library('upload', $config); 

    if ($this->upload->do_upload('userfile')) { 
    $response = array('upload_data' => $this->upload->data()); 
    $this->doResize($response['upload_data']['file_name']); 

    return $response; 
    } else { 
    $error = array('error' => $this->upload->display_errors()); 
    return false; 
    // var_dump($error); die(); // check errors 
    } 
} 

function doResize($filename) { 

    $sourcePath = './assets/images/user/' . $filename; 
    $targetPath = './assets/images/user/thumb_' . $filename; 

    $config_manip = array(
    'image_library' => 'gd2', 
    'source_image' => $sourcePath, 
    'new_image' => $targetPath, 
    'maintain_ratio' => true, 
    'width' => 100, 
    'height' => 100 
); 
    $this->image_lib->initialize($config_manip); 
    $this->load->library('image_lib', $config_manip); 

if (!$this->image_lib->resize()) { 
    // Check errors 
    echo $this->image_lib->display_errors(); 
    die(); 
} 
}