我可以設法將數據插入到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>
我現在可以上傳圖像的位置,也可以在tblaccount中的數據。我現在的問題是上傳按鈕可以上傳任何文件。如果它是空的,它的驗證是什麼?謝謝。 –
抱歉,對於遲到的回覆,因爲我已經在配置中檢查了允許的類型是jpg | jpeg | png | gif,所以只有這些文件纔會上傳,沒有其他的驗證,如果圖像上傳,如果它不是空的($ _ FILES [圖片'] ['名稱']。你可以檢查通過jquery或其他條件給用戶的錯誤不是空的。 – aishwarya