2016-11-08 83 views
0

我有形式上传图片在PHP中,当我保存路径和名称,名称返回表空间数据库中的空字段这里是我的代码和我的数据库中的值总是空的字段,请帮助我文件名返回空codeigniter

登记控制器

<?php 
/** 
* 
*/ 
class Registration extends CI_Controller { 

    function __construct() { 
     parent::__construct(); 
     //$this->load->library('upload'); 
     $this->load->helper('form'); 
    } 

    function index() { 
     $this->load->view('/registration/daftar'); 

    } 

    function add_user() { 
     $this->load->model('/daftar/Daftar_Model'); 
     $image_path = APPPATH. 'user_images/'; 
     $config['upload_path'] = $image_path; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size']  = '500'; 
     $config['max_height'] = '450'; 
     $config['max_width']  = '450'; 
     //$config['encrypt_name'] = TRUE; 
     $this->load->library('upload', $config); 
     $this->upload->initialize($config); 
     $path = $this->upload->data(); 
     $image_name1 = $path['file_name']; 
     $image_path1 = $path['file_path']; 
     //$this->upload->do_upload('foto'); 
     //$photo_data = $this->upload->data(); 

     $data = array(
      'username' => $this->input->post('username'), 
      'password' => $this->input->post('password'), 
      'email' => $this->input->post('email'), 
      'no_telepon' => $this->input->post('no_telepon'), 
      'alamat' => $this->input->post('alamat'), 
      'image_path' => $image_path1, 
      'image_name' => $image_name1 
      ); 

     $this->db->select('username'); 
     $this->db->where('username', $data['username']); 
     $result = $this->db->get('user'); 

     if($result->num_rows() > 0) { 
      echo "Udah ada"; 
      $this->output->set_header('refresh:2; url='.site_url("/registration")); 

     } else { 
      if(! $this->upload->do_upload('foto')) { 
       var_dump($this->upload->display_errors()); 
       //$this->load->view('/registration/daftar', $error); 
      } else { 
       //$data1 = array('upload_data' => $this->upload->data()); 
       $this->Daftar_Model->insert($data); 
       echo "Registrasi Berhasil"; 
       $this->output->set_header('refresh:2; url='.site_url("/login")); 
       //$this->load->view('/registration/daftar', $data1); 
      } 
     } 
    } 
} 

这是我的形式

<html> 
<head> 
    <title>Daftar</title> 
    <body> 
    <?php 
    ?> 
     <h1><center>Daftar</center></h1> 
     <?php echo form_open_multipart('registration/registration/add_user'); ?> 
     <center> 
      Username <br> 
      <input type="text" name="username"> <br> 
      Password <br> 
      <input type="password" name="password"> <br> 
      Email <br> 
      <input type="text" name="email"> <br> 
      No Telepon <br> 
      <input type="text" name="no_telepon"> <br> 
      Alamat <br> 
      <textarea name="alamat" rows="6" cols="23"></textarea> <br> 
      </form> 
      Foto 
      <input type="file" name="foto"><br><br><br> 
      <input type="submit" name="submit" value="Daftar"> 
     </center> 
     </form> 
    </body> 
</head> 
</html> 
+0

你不加载帮助程序和库上的视图加载它们在控制器或autoload.php – user4419336

+0

我已更新我的代码先生,但图像名称仍然返回空分贝,我很困惑,因为文件路径插入正确进入分贝,但为什么没有插入文件名 –

回答

0

你检查你函数add_user()

我建议你在添加到数据库之前检查上传状态。如果你的上传代码错误,他们会告诉你错误,然后你会知道在哪里解决它。它应该是:

if(!$this->upload->do_upload('foto')) 
{ 
    // Print the upload error 
    var_dump($this->upload->display_errors()); 
}else{ 
    $return = $this->upload->data(); 
    $image_name = $return['file_name']; 
    // your code add to database 
} 

希望得到这个帮助。

+0

我已经使用您的代码先生,但是当我插入图像名称到分贝,文件名仍然返回空字段,请参阅我更新的代码,文件路径插入成功分贝,但图像名称仍为空,没有错误代码显示 –

+0

@AntonPrioHutomo你能告诉我你的数据库的结构吗?也许它来自你的结构数据库 –