2016-04-19 2 views
1

Ich bin ziemlich neu in CodeIgniter, so dass ich nur bestimmte Aspekte von dem, was das Framework bieten kann, nutzen kann. In diesem Fall wollte ich dem Benutzer ermöglichen, ein Bild mit den restlichen Informationen hochzuladen. Ich habe versucht, Logik aus dem, was ich erstellt habe, mit Tutorials und Informationen, die ich hier gefunden habe, vergeblich zu kombinieren. Mit diesem Code werden derzeit alle Informationen auf den Server hochgeladen, aber es wird nichts hochgeladen, was mit dem vom Benutzer eingegebenen Bild zu tun hat. Also nehme ich an, dass ich etwas falsch mache. Jeder Rat wird geschätzt.Bild kann nicht mit dem Rest der eingegebenen Daten hochgeladen werden

function adduser(){ 
     $config['upload_path'] = './uploads'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_width'] = '1024'; 
     $config['max_height'] = '1024'; 
     $this->load->library('upload', $config); 

     $uinfo = $this->upload->data(); 
     $file = array(
      'img_name' => $uinfo['raw_name'], 
      'ext' => $uinfo['file_ext'] 
     ); 
     $fullfile = $file['img_name'].$file['file_ext']; 

     $data = array(
      'userid' => $this->session->userdata('userid'), 
      'name' => $this->input->post('name'), 
      'year' => $this->input->post('year'), 
      'title' => $this->input->post('title'), 
      'image' => $fullfile 
     ); 
     $this->db->insert('user', $data); 
     redirect('user/index'); 
} 
+0

Verwenden CI Fehlerberichterstattung https: //www.codeigniter. com/user_guide/general/errors.html wenn Sie es nicht schon tun, und sehen, ob etwas daraus wird. –

+0

@thelonalddoge Sie haben den Uploadvorgang von Codeigniter verpasst. Sollte '$ this-> upload-> do_upload()' verwenden. Weitere Informationen finden Sie unter [this] (https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html). – Yash

Antwort

1

versuchen Sie bitte die folgenden

public function adduser(){ 
    { 
      //This is the directory to which we upload our image 
      //full permission is required for this folder 
      //FCPATH is a constant defined in index.php - its the relative path to project root 
      $config['upload_path'] = FCPATH.'uploads/'; 
      //Validation - Only gif jpg png extensions are allowed 
      $config['allowed_types'] = 'gif|jpg|png'; 
      //unique file name 
      $file_name   = time(); 
      $config['file_name'] = $file_name; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '1024'; 

      $this->load->library('upload', $config); 
      //checking file upload success 

      if (! $this->upload->do_upload('image')) //here image is your file input name 
      { 
       //error in file uploading 
       $message = $this->upload->display_errors(); 
       echo "<pre>"; 
       print_r($message); 
       exit; 
      } 
      else 
      { 
       //successfully uploaded the image 
       $message = 'file uploaded successfully'; 
       //getting full file upload information 
       $result = $this->upload->data(); 
       $data = array(
        'userid' => $this->session->userdata('userid'), 
        'name' => $this->input->post('name'), 
        'year' => $this->input->post('year'), 
        'title' => $this->input->post('title'), 
        'image' => $result['file_name'] 
       ); 
       $this->db->insert('user', $data); 

      } 

      redirect('user/index'); 
    } 

in der oben habe ich verwendet "Bild" als Dateieingabefeld Name

+0

Danke! Das funktioniert perfekt. Jetzt kann ich mit der Löschfunktion für das Bild fortfahren. –

0
Try This 

    function adduser(){ 
      $config['upload_path'] = './uploads'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '1024'; 
      $this->load->library('upload', $config); 

      if (!$this->upload->do_upload('inputname')) { 
       echo $this->upload->display_errors(); 
      } else { 
       $uinfo = $this->upload->data(); 
       $file = array(
        'img_name' => $uinfo['raw_name'], 
        'ext' => $uinfo['file_ext'] 
       ); 
       $fullfile = $file['img_name'].$file['file_ext']; 

       $data = array(
        'userid' => $this->session->userdata('userid'), 
        'name' => $this->input->post('name'), 
        'year' => $this->input->post('year'), 
        'title' => $this->input->post('title'), 
        'image' => $fullfile 
       ); 
       $this->db->insert('user', $data); 
       redirect('user/index'); 
      } 
    } 

Note: "inputname" is your HTML input file tag 
Verwandte Themen