2017-05-07 13 views
0

codeigniterでアップロードフォームを作成しようとすると少し問題があります。 私が間違っていることを確認していない、他のすべてがデータベースに正しく入っています。私はドキュメンテーションを見て、いくつかのことを試しましたが、私はそれ以上のことはありません..すべてのフィードバックは非常に感謝しています!
ありがとうございます。Codeigniterイメージのアップロードとデータベースへのパス

モデル:

public function set_newstudent() 
    { 
     $this->load->helper('url'); 

     $slug = url_title($this->input->post('naam'), 'dash', TRUE); 

       $config['upload_path'] = '/file_path/'; 
       $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
       $this->load->library('upload', $config); 
       $this->upload->data('full_path');); 
       $data_upload_files = $this->upload->data(); 

       $image = $data_upload_files[full_path]; 

       $data = array(

      'naam' => $this->input->post('naam'), 
      'voornaam' => $this->input->post('voornaam'), 
      'id' => $slug, 
      'text' => $this->input->post('text'), 
      'picture'=>$this->input->post('picture') 
    ); 

     return $this->db->insert('student', $data); 
    } 

コントローラ:

public function create() 
      { 
       $this->load->helper('form'); 
       $this->load->library('form_validation'); 

       $data['title'] = 'Create a new Student'; 

       $this->form_validation->set_rules('naam', 'Naam', 'required'); 
       $this->form_validation->set_rules('voornaam', 'Voornaam', 'required'); 
       $this->form_validation->set_rules('text', 'Text', 'required'); 

       if ($this->form_validation->run() === FALSE) 
        { 
        $this->load->view('templates/header', $data); 
        $this->load->view('students/create'); 
        $this->load->view('templates/footer'); 

        } 
       else 
        { 
        $this->student_model->set_newstudent(); 
        $this->load->view('students/success'); 
        } 
      } 

ビュー:

<?php echo validation_errors(); ?> 


<?php echo form_open_multipart('student/create');?> 

<div class="form-group"> 
<label for="naam">Naam</label><br> 
<input type="input" name="naam" class="form-control" /><br /> 
</div> 
<div class="form-group"> 
<label for="voornaam">Voornaam</label><br> 
<input type="input" name="voornaam" class="form-control"/><br /> 
</div> 
<div class="form-group"> 
<label for="text">Vertel iets over jezelf:</label><br> 
<textarea name="text" class="form-control" rows="5"></textarea><br /> 
</div> 

<div class="form-group"> 
<label for="text">Kies een profiel foto:</label><br> 
<input type="file" name="userfile" class="btn btn-default btn-file" /> 

</div> 
<input type="submit" class="btn btn-success" name="submit" 
value="Create student" style="width:100%;margin-bottom:1%" /> 

</form> 
+0

これはなんですか? '$ this-> upload-> create' –

+0

私の間違い申し訳ありません、$ this-> upload-> data( 'full_path'); – Moya

+0

どのバージョンのcodeigniterを使用していますか? –

答えて

2

私が見るように、存在しない

$this->upload->do_upload() 

アップロードを実行する責任機能だと、それはあなたのコードではありません、あなたはこの読むことをお勧めします: File uploading Class

UPDATE

をあなたのコードは次のようになります、

コントローラ:

public function create(){ 
$this->load->helper('form'); 
$this->load->library('form_validation'); 
$data['title'] = 'Create a new Student'; 
$this->form_validation->set_rules('naam', 'Naam', 'required'); 
$this->form_validation->set_rules('voornaam', 'Voornaam', 'required'); 
$this->form_validation->set_rules('text', 'Text', 'required'); 
if ($this->form_validation->run() === FALSE){ 
    $this->load->view('templates/header', $data); 
    $this->load->view('students/create'); 
    $this->load->view('templates/footer'); 
}else{ 
    // Upload the files then pass data to your model 
    $config['upload_path'] = '/file_path/'; 
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $this->load->library('upload', $config); 

    if (!$this->upload->do_upload('userfile')){ 
     // If the upload fails 
     echo $this->upload->display_errors('<p>', '</p>'); 
    }else{ 
     // Pass the full path and post data to the set_newstudent model 
     $this->student_model->set_newstudent($this->upload->data('full_path'),$this->input->post()); 
     $this->load->view('students/success'); 
    } 
} 
} 

モデル:

public function set_newstudent($path,$post){ 
$data = array( 
'naam' => $post['naam'], 
'voornaam' => $post['voornaam'], 
'text' => $post['text'], 
'picture'=>$path 
); 

return $this->db->insert('student', $data); 
} 
+0

ご回答ありがとうございます。これをcreate()関数に追加できますか?それとも別の機能でなければならないのでしょうか? – Moya

+0

作成のような機能はありません。どこでこの機能を見つけましたか教えてください。 –

+0

コントローラで – Moya

0

アップロードライブラリを使用しているコードに問題があります。間違った機能を使ってファイルをアップロードしています。以下は正しいコードです:

 $config['upload_path'] = '/file_path/'; 
     $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
     $this->load->library('upload', $config); 

     // Check file uploaded or not 
     if ($this->upload->do_upload('userfile')) { 
      $data_upload_files = $this->upload->data(); 
      $image = $data_upload_files[full_path]; 
     } else { 
      // possibly do some clean up ... then throw an error 

     } 

このコードは有効です。

+0

申し訳ありませんが、コントローラのコードを上記に変更しましたが、まだ結果が得られません – Moya

+0

画面に表示される正確なエラーを知ることができますか? –

+0

事は、私は間違いがないということです。ユーザーが作成され、フィールド名と姓がデータベースに書き込まれていますが、添付されていても画像が表示されません – Moya

関連する問題