[eluser]Leon Stafford[/eluser]
Here's another example for people searching.
This one's errors are working and the class is set up to be more modular for saving form results with multiple uploads and be re-useable for multiple forms by changing config arrays:
class file:
Code:
<?php
class Save extends Controller {
public $results = array();
function Save()
{
parent::Controller();
$this->load->helper(array('form', 'url','html'));
$this->load->library('form_validation');
}
function index()
{
$this->load->view('upload_form');
}
function file_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '3000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
$number_of_files = 4;
for ($file_counter = 0; $file_counter < $number_of_files; $file_counter +=1){
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('userfile'.$file_counter))
{
$this->results['result'.$file_counter]['error'] = array($this->upload->display_errors('<p style="color:red;">', '</p>'));
$this->results['result'.$file_counter]['success'] = array('');
}
else
{
$this->results['result'.$file_counter]['success'] = array($this->upload->data());
$this->results['result'.$file_counter]['error'] = array('');
}
}
}
public $validation_config = array();
public $shop_validation = array(
array(
'field' => 'username',
'label' => 'Username',
'rules' => 'trim|required|prep_for_form'
),
array(
'field' => 'password',
'label' => 'Password',
'rules' => 'trim|required|matches[passconf]|md5'
),
array(
'field' => 'passconf',
'label' => 'Password Confirmation',
'rules' => 'trim|required'
),
array(
'field' => 'email',
'label' => 'Email',
'rules' => 'trim|required|valid_email'
)
);
function validate_form()
{
$this->form_validation->set_error_delimiters('<p style="color:red;">', '</p>');
$this->form_validation->set_rules($this->validation_config);
if ($this->form_validation->run() == FALSE)
{
$this->load->view('upload_form', $this->results);
}
else
{
$this->load->view('form_success');
}
}
function save_shop()
{
$this->file_upload();
$this->validation_config = $this->shop_validation;
$this->validate_form();
}
}
?>
view file:
Code:
<html>
<head>
<title>Upload Form</title>
</head>
<body>
HTML code starts here
<?php
$total_files = 4;
?>
<?php
$attributes = array('class' => 'email_form', 'id' => 'upload_form');
echo form_open_multipart('save/save_shop',$attributes);?>
<!-- add for X do Y loop to set all image sections dynamically re number of file fields -->
<?php for ($x = 0; $x < $total_files; $x++){?>
<input type="file" name="userfile<?php echo $x; ?>" size="20" />
<br />
<?php if (isset(${"result$x"})){
if (${"result$x"}['error'][0]!=''){
//display error
echo ${"result$x"}['error'][0];
} else {
echo ${"result$x"}['success'][0]['file_name'];
}
}?>
<br /><br />
<?php } ?>
<h5>Username</h5>
<?php echo form_error('username'); ?>
<input type="text" name="username" value="<?php echo set_value('username'); ?>" size="50" />
<h5>Password</h5>
<?php echo form_error('password'); ?>
<input type="text" name="password" value="<?php echo set_value('password'); ?>" size="50" />
<h5>Password Confirm</h5>
<?php echo form_error('passconf'); ?>
<input type="text" name="passconf" value="<?php echo set_value('passconf'); ?>" size="50" />
<h5>Email Address</h5>
<?php echo form_error('email'); ?>
<input type="text" name="email" value="<?php echo set_value('email'); ?>" size="50" />
<!-- change array structure so that fields are always created, just empty so can call directly without isset -->
<br />
<input type="submit" value="upload" />
</form>
</body>
</html>