2017-02-09 3 views
2

데이터를 tblaccount에 삽입 할 수 있습니다. 그러나 문제는 사진을 업로드 할 수 없다는 것입니다. tblaccount에는 이름, 성, 이메일, 부서, 사용자 이름 및 비밀번호의 올바른 데이터가 포함되어 있지만 일부 이미지를 업로드하더라도 사진은 공백으로 남아 있습니다.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']) 다음 va가 필요함 두 번째로 디렉토리가 생성되었는지 여부를 확인하고 777 권한을 제공해야합니다. 이 두 가지 검사 만 추가하여 코드를 테스트했습니다. 도움을 받으십시오.

$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이므로이 파일들만 업로드되고, 이미지가 비어 있지 않은 경우 이미지가 업로드되면 유효성이 검사됩니다! empty ($ _ FILES [ 그림 '] ['이름 ']. 당신은 jquery를 통해 확인하거나 다른 조건에서 비어 있지 않은 사용자에게 오류를 줄 수 있습니다. – 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 { 

} 
관련 문제