2016-05-24 66 views
0

我的控制器代码我的图片不会保存到我的上传/图片文件夹

期待您的帮助!谢谢:

public function index() 
{ 
    $registration = $this->Registration_model->get_all(); 

    $data = array(
     'registration_data' => $registration 
    ); 

    $this->load->view('registration/registration_list', $data); 

} 
public function do_upload() 
    { 
      $config['upload_path']   = './uploads/pictures/'; 
      $config['allowed_types']  = 'gif|jpg|png'; 
      $config['max_size']    = 10000000; 
      $config['max_width']   = 10240000; 
      $config['max_height']   = 76800000; 

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

      if (! $this->upload->do_upload('Image')) 
      { 
        $error = array('error' => $this->upload->display_errors()); 

        $this->load->view('registration_form', $error); 
      } 
      else 
      { 
        $data = array('upload_data' => $this->upload->data()); 
        return $data['upload_data']['file_name']; 
      } 
    } 
public function read($id) 
{ 
    $row = $this->Registration_model->get_by_id($id); 
    if ($row) { 
     $data = array(
    'id' => $row->id, 
    'firstName' => $row->firstName, 
    'lastName' => $row->lastName, 
    'gender' => $row->gender, 
    'address' => $row->address, 
    'dob' => $row->dob, 
    'Image' => $row->Image, 
    ); 
     $this->load->view('registration/registration_read', $data); 
    } else { 
     $this->session->set_flashdata('message', 'Record Not Found'); 
     redirect(site_url('registration')); 
    } 
} 

public function create() 
{ 
    $data = array(
     'button' => 'Create', 
     'action' => site_url('registration/create_action'), 
    'id' => set_value('id'), 
    'firstName' => set_value('firstName'), 
    'lastName' => set_value('lastName'), 
    'gender' => set_value('gender'), 
    'address' => set_value('address'), 
    'dob' => set_value('dob'), 
    'Image' =>set_value('image'), 
); 
    $this->load->view('registration/registration_form', $data); 
     } 

public function create_action() 
{ 
    $this->_rules(); 

    if ($this->form_validation->run() == FALSE) { 
     $this->create(); 
    } else { 
     $data = array(
    'firstName' => $this->input->post('firstName',TRUE), 
    'lastName' => $this->input->post('lastName',TRUE), 
    'gender' => $this->input->post('gender',TRUE), 
    'address' => $this->input->post('address',TRUE), 
    'dob' => $this->input->post('dob',TRUE), 
    'Image' => $this->input->post('Image',TRUE), 
    ); 

     $this->Registration_model->insert($data); 
     $this->session->set_flashdata('message', 'The New Record was Successfuly Added'); 
     redirect(site_url('registration')); 
    } 
} 

我的视图代码

<div class="form-group"> 
    <label for="date">Dob <?php echo form_error('dob') ?></label> 
    <input type="text" class="form-control" name="dob" id="dob" placeholder="Dob" value="<?php echo $dob; ?>" /> 
</div> 
<div class="form-group"> 
    <label for="varchar">Image <?php echo form_open_multipart('upload/do_upload');?></label> 
    <input type="file" class="form-control" name="Image" id="Image" height="50" /> 
    <br/> 
</div> 
    <br/> 
<input type="hidden" name="id" value="<?php echo $id; ?>" /> 
<button type="submit" class="btn btn-primary"name="userSubmit"><?php echo $button ?></button> 

我的模型

class Registration_model extends CI_Model 
{ 

    public $table = 'registration'; 
    public $id = 'id'; 
    public $order = 'DESC'; 

    function __construct() 
    { 
     parent::__construct(); 
    } 

    // get all 
    function get_all() 
    { 
     $this->db->order_by($this->id, $this->order); 
     return $this->db->get($this->table)->result(); 
    } 

    // get data by id 
    function get_by_id($id) 
    { 
     $this->db->where($this->id, $id); 
     return $this->db->get($this->table)->row(); 
    } 

    // get total rows 
    function total_rows($q = NULL) { 
     $this->db->like('id', $q); 
    $this->db->or_like('firstName', $q); 
    $this->db->or_like('lastName', $q); 
    $this->db->or_like('gender', $q); 
    $this->db->or_like('address', $q); 
    $this->db->or_like('dob', $q); 
    $this->db->or_like('Image', $q); 
    $this->db->from($this->table); 
     return $this->db->count_all_results(); 
    } 

    // get data with limit and search 
    function get_limit_data($limit, $start = 0, $q = NULL) { 
     $this->db->order_by($this->id, $this->order); 
     $this->db->like('id', $q); 
    $this->db->or_like('firstName', $q); 
    $this->db->or_like('lastName', $q); 
    $this->db->or_like('gender', $q); 
    $this->db->or_like('address', $q); 
    $this->db->or_like('dob', $q); 
    $this->db->or_like('Image', $q); 
    $this->db->limit($limit, $start); 
     return $this->db->get($this->table)->result(); 
    } 

    // insert data 
    function insert($data) 
    { 
     $this->db->insert($this->table, $data); 
    } 

    // update data 
    function update($id, $data) 
    { 
     $this->db->where($this->id, $id); 
     $this->db->update($this->table, $data); 
    } 

    // delete data 
    function delete($id) 
    { 
     $this->db->where($this->id, $id); 
     $this->db->delete($this->table); 
    } 

} 
+2

检查的正确路径第一,比文件夹权限 –

+0

检查第一路'$配置[“upload_path”] =“./uploads/pictures /”;' – RJParikh

+0

你需要调试代码并找到错误,你得到的基础上的代码,这是不可能找出问题 –

回答

0

你好你可以试试这个

$config['upload_path'] = 'ADD HERE YOUR SITE PATH TO PICTURE'; // Like SITE_PATH .'/uploads/pictures/'; 

并打印$ config ['upload_path']并检查它是否正确您的上传图片路径

0

我以前遇到同样的问题。根据您的服务器,您可能希望从上传路径中删除./。

public function do_upload() 
{ 
    $config['upload_path'] = 'uploads/pictures/'; 
+0

我已经尝试过,但仍然没有工作:'( –

0

改变你的看法如下。您不关闭形式

<?php echo form_open_multipart('upload/do_upload');?> 
<div class="form-group"> 
<label for="date">Dob <?php echo form_error('dob') ?></label> 
    input type="text" class="form-control" name="dob" id="dob" placeholder="Dob" value="<?php echo $dob; ?>" /> 
</div> 
<div class="form-group"> 
<label for="varchar">Image </label> 
<input type="file" class="form-control" name="Image" id="Image" height="50" /> 
<br/> 
</div> 
<br/> 
<input type="hidden" name="id" value="<?php echo $id; ?>" /> 
<button type="submit" class="btn btn-primary"name="userSubmit"><?php echo $button ?></button> 
<?= form_close()?> 
+0

它不工作:( –