• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
upload and validation

#1
[eluser]Rvnikita[/eluser]
Hi!
I read a lot on this forum about problems in CI with using uploading files and validation fields at the same time, found a lot of discussions and tried to merge information in one topic.

The last version contains 2 file upload fileds and one text field:

Controller:
Code:
<?php
class upload extends Controller {
    
    function index()
    {
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('text', 'text', 'require');
        $this->form_validation->set_rules('userfile_fake', 'userfile_fake', 'callback_file_check[TRUE]');
        $this->form_validation->set_rules('userfile_fake2', 'userfile_fake2', 'callback_file_check[TRUE]');
        
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('upload');
        }
        else
        {
            echo "OK";
        }
    }
    
    function file_check($file, $required)
    {
        $this->form_validation->set_message('file_check', '');
        if (@$_FILES['userfile']['error'] == 4 && $required == 'FALSE')
        {
            return TRUE;
        }
        else
        {
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size']    = '10000';
            $config['max_width']  = '1024';
            $config['max_height']  = '768';
            
            $this->load->library('upload', $config);
            
            if (!$this->upload->do_upload($file)) //we have problems
            {
                return FALSE;
            }    
            else
            {
                return TRUE;
            }
        }
    }
}
?>

Viewer:
Code:
<html>
<head>
<title>My Form</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

</head>
<body>

<?php echo validation_errors(); ?>
<?php echo $this->upload->display_errors(); ?>


<?php echo form_open_multipart('upload'); ?>

<input type="text" name="text" value="<?php echo set_value('text'); ?>" size="50" /><br />

<input type="hidden" name="userfile_fake2" value="userfile2">
<input type="file" name="userfile2" size="20" /><br />

<input type="hidden" name="userfile_fake" value="userfile">
<input type="file" name="userfile" size="20" />

<div>&lt;input type="submit" value="Submit" /&gt;&lt;/div>

&lt;/form&gt;

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

I have some ideas to upgrade this code, may be later...
By the way, i don't understand why CI core coders don't intigrated file upload in validation :\
May be because it's not so easy...


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.