Welcome Guest, Not a member yet? Register   Sign In
Latavish's Multiple Image Upload with Thumbnail Generation
#13

[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();
    }
}
?&gt;

view file:

Code:
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Upload Form&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
HTML code starts here


&lt;?php
$total_files = 4;
?&gt;

&lt;?php
$attributes = array('class' => 'email_form', 'id' => 'upload_form');
echo form_open_multipart('save/save_shop',$attributes);?&gt;
&lt;!-- add for X do Y loop to set all image sections dynamically re number of file fields --&gt;
&lt;?php for ($x = 0; $x < $total_files; $x++){?&gt;
&lt;input type="file" name="userfile&lt;?php echo $x; ?&gt;" size="20" /&gt;
<br />
&lt;?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'];
            }
            
            
        }?&gt;
<br /><br />
&lt;?php } ?&gt;
<h5>Username</h5>
&lt;?php echo form_error('username'); ?&gt;
&lt;input type="text" name="username" value="&lt;?php echo set_value('username'); ?&gt;" size="50" /&gt;

<h5>Password</h5>
&lt;?php echo form_error('password'); ?&gt;
&lt;input type="text" name="password" value="&lt;?php echo set_value('password'); ?&gt;" size="50" /&gt;

<h5>Password Confirm</h5>
&lt;?php echo form_error('passconf'); ?&gt;
&lt;input type="text" name="passconf" value="&lt;?php echo set_value('passconf'); ?&gt;" size="50" /&gt;

<h5>Email Address</h5>
&lt;?php echo form_error('email'); ?&gt;
&lt;input type="text" name="email" value="&lt;?php echo set_value('email'); ?&gt;" size="50" /&gt;


&lt;!-- change array structure so that fields are always created, just empty so can call directly without isset --&gt;
<br />

&lt;input type="submit" value="upload" /&gt;

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;


Messages In This Thread
Latavish's Multiple Image Upload with Thumbnail Generation - by El Forum - 02-09-2009, 02:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB