2017-02-09 60 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,所以只有這些文件纔會上傳,沒有其他的驗證,如果圖像上傳,如果它不是空的($ _ 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 { 

}