2017-02-09 5 views
2

私はtblaccountにデータを挿入することができます。しかし、問題は画像をアップロードすることができないことです。 tblaccountにはファーストネーム、ラストネーム、Eメール、部門、ユーザー名、パスワードのデータが含まれていますが、画像をアップロードしても空白のままです。codeigniterを使って画像をアップロードするには?

更新:画像はフォルダにアップロードできるようになりましたが、tblaccountには画像がアップロードできなくなりました。空白のデータのみを表示します。

signup.phpコントローラ:

public function index() 
{ 
    // set form validation rules 
    $this->form_validation->set_rules('firstname', 'First Name', 'trim|required|alpha|min_length[3]|max_length[30]'); 
    $this->form_validation->set_rules('lastname', 'Last Name', 'trim|required|alpha|min_length[3]|max_length[30]'); 
    $this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email|is_unique[tblaccount.Email]'); 
    $this->form_validation->set_rules('department', 'Department', 'trim|required|alpha|min_length[3]|max_length[30]'); 
    $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha|min_length[3]|max_length[30]|is_unique[tblaccount.Username]'); 
    $this->form_validation->set_rules('password', 'Password', 'trim|required'); 
    $this->form_validation->set_rules('cpassword', 'Confirm Password', 'trim|required|matches[password]'); 
    $this->form_validation->set_rules('picture', 'Image', 'trim|required'); 

    // submit 
    if ($this->form_validation->run() == FALSE) 
    { 
     // fails 
     $this->load->view('signup_view'); 
    } 
    else 
    { 
     if(!empty($_FILES['picture']['name'])) 
     { 
      $config['upload_path'] = './uploads/images/'; 
      $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
      $config['max_size'] = 10000000; 
      $config['file_name'] = $_FILES['picture']['name']; 

      //Load upload library and initialize configuration 
      $this->load->library('upload',$config); 
      $this->upload->initialize($config); 

      if($this->upload->do_upload('picture')) 
      { 
       $uploadData = $this->upload->data(); 
       $picture = $uploadData['file_name']; 
      } 
      else 
      { 
       $picture = ''; 
       $error = array('error' => $this->upload->display_errors()); 
       echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '".base_url("index.php/signup")."';</script>"; 
      } 
     } 
     else 
     { 
      $picture = ''; 
     } 
      $data = array(
       'First_Name' => $this->input->post('firstname'), 
       'Last_Name' => $this->input->post('lastname'), 
       'Email' => $this->input->post('email'), 
       'Department' => $this->input->post('department'), 
       'Username' => $this->input->post('username'), 
       'Password' => $this->input->post('password'), 
       'Picture' => $picture 
      ); 

      if ($this->account_model->insert($data)) 
      { 
       $this->session->set_flashdata('msg','<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>'); 
       redirect('login'); 
      } 
      else 
      { 
       // error 
       $this->session->set_flashdata('msg','<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>'); 
       redirect('signup'); 
      } 
    } 
} 

signup.phpビュー:アップロードパスに

<div class="row"> 
     <div class="col-md-4 col-md-offset-4 well"> 
      <?php echo form_open_multipart('signup');?> 
       <legend>Signup</legend> 

        <div class="form-group"> 
         <label for="name">First Name</label> 
          <input class="form-control" name="firstname" placeholder="First Name" type="text" value="<?php echo set_value('First_Name');?>"/> 
          <span class="text-danger"><?php echo form_error('firstname'); ?></span> 
        </div>   

        <div class="form-group"> 
         <label for="name">Last Name</label> 
          <input class="form-control" name="lastname" placeholder="Last Name" type="text" value="<?php echo set_value('Last_Name');?>"/> 
          <span class="text-danger"><?php echo form_error('lastname'); ?></span> 
        </div> 

        <div class="form-group"> 
         <label for="email">Email Address</label> 
          <input class="form-control" name="email" placeholder="Email Address" type="text" value="<?php echo set_value('Email');?>"/> 
          <span class="text-danger"><?php echo form_error('email'); ?></span> 
        </div> 

        <div class="form-group"> 
         <label for="email">Department</label> 
          <input class="form-control" name="department" placeholder="Department" type="text" value="<?php echo set_value('Department');?>"/> 
          <span class="text-danger"><?php echo form_error('department'); ?></span> 
        </div> 

        <div class="form-group"> 
         <label for="email">Username</label> 
          <input class="form-control" name="username" placeholder="Username" type="text" value="<?php echo set_value('Username');?>"/> 
          <span class="text-danger"><?php echo form_error('username'); ?></span> 
        </div> 

        <div class="form-group"> 
         <label for="subject">Password</label> 
          <input class="form-control" name="password" placeholder="Password" type="password"/> 
          <span class="text-danger"><?php echo form_error('password'); ?></span> 
        </div> 

        <div class="form-group"> 
         <label for="subject">Confirm Password</label> 
          <input class="form-control" name="cpassword" placeholder="Confirm Password" type="password"/> 
          <span class="text-danger"><?php echo form_error('cpassword'); ?></span> 
        </div> 

        <div class="form-group"> 
         <label for="subject">Profile Picture:</label> 
          <input class="form-control" name="picture" accept="image/*" type="file"/> 
          <span class="text-danger"><?php echo form_error('picture'); ?></span> 
        </div> 

        <div class="form-group"> 
         <button name="submit" type="submit" class="btn btn-info">Signup</button> 
         <button name="cancel" type="reset" class="btn btn-info">Cancel</button> 
        </div> 

      <?php echo form_close(); ?> 
     </div> 
    </div> 

答えて

0

削除するには、画像フィールドに渡すことが許可されていないとして、あなたは空のファイルフィールドを(チェックインするときに、検証チェックから必要な!空($ _ FILES [「絵」はここで説明する必要があります] ['name'])then vaが必要ですリッドは既に内部でチェックされています。次に、ディレクトリが作成されているかどうかをチェックし、777の許可を与えなければなりません。私はこれらの2つのチェックだけを追加してコードをテストしました。希望することができます

$this->form_validation->set_rules('picture', 'Image', 'trim'); /*Change in this line -- remove required*/ 
     if ($this->form_validation->run()) { 
      if (!empty($_FILES['picture']['name'])) { 
       $config['upload_path'] = 'uploads/images/'; 
       /*add 777 permission to directory*/ 
       if (!is_dir($config['upload_path'])) { 
        mkdir($config['upload_path'], 0777, TRUE); 
       } 
       $config['allowed_types'] = 'jpg|jpeg|png|gif'; 
       $config['max_size'] = 10000000; 
       $config['file_name'] = $_FILES['picture']['name']; 

       //Load upload library and initialize configuration 
       $this->load->library('upload', $config); 
       $this->upload->initialize($config); 

       if ($this->upload->do_upload('picture')) { 
        $uploadData = $this->upload->data(); 
        $picture = $uploadData['file_name']; 
       } else { 
        $picture = ''; 
        $error = array('error' => $this->upload->display_errors()); 
        echo "<script>alert('JPG, JPEG, PNG and GIF type of file only is allowed and atleast 10MB of size');window.location = '" . base_url("index.php/signup") . "';</script>"; 
       } 
      } else { 
       $picture = ''; 
      } 
      $data = array(
       'image' => $picture 
      ); 

      $this->write_conn->db->insert('test', $data); 
      if ($this->write_conn->db->insert('test', $data)) { 
       $this->session->set_flashdata('msg', '<div class="alert alert-success text-center">You are successfully registered! Please login to access your profile!</div>'); 
      } else { 
       // error 
       $this->session->set_flashdata('msg', '<div class="alert alert-danger text-center">Oops! Error. Please try again later!!!</div>'); 
      } 
     } 
+0

ここでは、画像をtblaccountの位置にアップロードすることもできます。私の問題は今、アップロードボタンがドキュメントのようなものをアップロードできることです。それが空であれば、その検証は何ですか?ありがとう。 –

+0

返信が遅いので申し訳ありませんが、設定で許可されているタイプはjpg | jpeg | png | gifなので、これらのファイルだけがアップロードされ、空でない場合は画像がアップロードされるかどうかが検証されます($ _ FILES [画像]] ['名前']。あなたはjqueryかelse条件のいずれかでチェックすることができます。 – aishwarya

0

$config['upload_path'] = 'uploads/images/'; 

$config['upload_path'] = './uploads/images/'; 
$config['upload_path'] = FCPATH . '/uploads/images/'; 
をお試しください

そして、私は(form_open_multipartを使用)、フォームhelperは別のノートには、フォルダの正しい権限

のみ最初signup.phpSignup.phpだろうどこがあなたのファイルとクラス名が正しい命名したことを確認してくださいことを確認してください手紙は、クラスで大文字とファイル名がhttps://www.codeigniter.com/user_guide/general/styleguide.html#file-naming

<?php 

class Signup extends CI_Controller { 

} 
関連する問題