我试图将图像添加到文件夹和数据库的路径,但我试图弄清楚如何去做但没有任何工作。请帮忙。 这是我的控制器
function index()
{
$this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
$this->form_validation->set_rules('photo', 'photo', 'required|trim|xss_clean');
$this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('registration');
}
else // passed validation proceed to post success logic
{
// build array for the model
$form_data = array(
'username' => set_value('username'),
'photo' => set_value('photo'),
'password' => set_value('password')
);
// run insert model to write data to db
if ($this->kint_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
redirect('user_registration/success'); // or whatever logic needs to occur
}
else
{
echo 'An error occurred saving your information. Please try again later';
// Or whatever error handling is necessary
}
}
}
function success()
{
echo 'User registered successfully';
}
这是我的模特
class Kint_model extends CI_Model {
function __construct()
{
parent::__construct();
}
function SaveForm($form_data)
{
$this->db->insert('users', $form_data);
if ($this->db->affected_rows() == '1')
{
return TRUE;
}
return FALSE;
}
}
这是我的观点
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open('user_registration', $attributes); ?>
<p>
<label for="username">username <span class="required">*</span></label>
<?php echo form_error('username'); ?>
<br /><input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>" />
</p>
<p>
<label for="photo">photo <span class="required">*</span></label>
<?php echo form_error('photo'); ?>
<br /><input id="photo" type="text" name="photo" value="<?php echo set_value('photo'); ?>" />
</p>
<p>
<label for="password">password <span class="required">*</span></label>
<?php echo form_error('password'); ?>
<br /><input id="password" type="text" name="password" value="<?php echo set_value('password'); ?>" />
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
答案 0 :(得分:0)
控制器:
function index()
{
$this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
$this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');
$this->form_validation->set_error_delimiters('<br /><span class="error">', '</span>');
if ($this->form_validation->run() == FALSE) // validation hasn't been passed
{
$this->load->view('registration');
}
else
{
$form_data = array(
'username' => $this->input->post('username'),
'photo' => '',
'password' => $this->input->post('password')
);
$UploadPreferences = array(
'upload_path' => './uploads/fullsize',
'allowed_types' => 'gif|jpg|png',
'encrypt_name' => TRUE
);
$this->upload->initialize($UploadPreferences);
$this->upload->do_upload('photo');
$photoInfo = $this->upload->data();
$form_data['photo'] = $photoInfo['full_path'];
if ($this->kint_model->SaveForm($form_data) == TRUE) // the information has therefore been successfully saved in the db
{
redirect('user_registration/success'); // or whatever logic needs to occur
}
else
{
echo 'An error occurred saving your information. Please try again later';
// Or whatever error handling is necessary
}
}
}
function success()
{
echo 'User registered successfully';
}
查看:
<?php // Change the css classes to suit your needs
$attributes = array('class' => '', 'id' => '');
echo form_open('user_registration', $attributes); ?>
<p>
<label for="username">username <span class="required">*</span></label>
<?php echo form_error('username'); ?>
<br /><input id="username" type="text" name="username" value="<?php echo set_value('username'); ?>" />
</p>
<p>
<label for="photo">photo <span class="required">*</span></label>
<?php echo form_error('photo'); ?>
<br /><input type="file" name="photo" multiple="multiple" />
</p>
<p>
<label for="password">password <span class="required">*</span></label>
<?php echo form_error('password'); ?>
<br /><input id="password" type="text" name="password" value="<?php echo set_value('password'); ?>" />
</p>
<p>
<?php echo form_submit( 'submit', 'Submit'); ?>
</p>
<?php echo form_close(); ?>
未经测试。
另请阅读:
http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
也:var_dump($this->upload->data());
。