Part of the EllisLab Network
   
7 of 7
7
Extended form_validation: now with file checking :)
Posted: 23 January 2010 02:33 PM   [ Ignore ]   [ # 61 ]  
Summer Student
Total Posts:  15
Joined  03-03-2008
GodsHand - 14 October 2009 09:28 PM

Any reason why an error message isn’t being displayed when no file is selected?

CONTROLLER

$this->form_validation->set_rules('userfile','Image','file_required'); 

VIEW

<class="input_text">
<
label for="userfile">Select Photo</label>
<
input type="file" name="userfile" id="userfile" />
<?php if (form_error('userfile')): echo '<span class="error">'.form_error('userfile').'</span>'; endif; ?>
</p

I have the same problem as this. It’s weird that when you use

<?php echo validation_errors(); ?> 

the error is there.

I used this dirty hack for the time being:

<?php if(strpos(validation_errors(), "No file selected") !== falseecho '<p>No file selected</p>'} ?> 

And placed it just underneath

<?php echo form_error('userfile'?> 

. This way all the other errors work fine.

Profile
 
 
Posted: 09 February 2010 12:30 AM   [ Ignore ]   [ # 62 ]  
Research Assistant
Avatar
RankRankRank
Total Posts:  743
Joined  09-11-2008

nice extension i really like it but one thing i want to ask can we use it for multiple file upload or array of files uploading. for example

<input type="file" name="upload[]" />
<
input type="file" name="upload[]" />
<
input type="file" name="upload[]" /> 

will it work for it.

 Signature 

CI,JQuery,Google Maps | widget with CI loader | Thumbnail, Image Resize, Image Crop Helper | CI shortcode

Profile
 
 
Posted: 23 February 2010 03:32 PM   [ Ignore ]   [ # 63 ]  
Summer Student
Total Posts:  2
Joined  12-03-2009

does anyone know why file_max_size[xxx] isnt working? im using this:

$this->form_validation->set_rules('blog_banner''Blog Banner''file_required|file_allowed_type[image]|file_max_size[100KB]'); 

everything but file size max works can anybody help me?

thanks guys

/* update */

found my problem lol using it wrong way around was using file_max_size when its file_size_max lol :D spent hours banging me head with this too lol

Profile
 
 
Posted: 02 September 2010 02:41 PM   [ Ignore ]   [ # 64 ]  
Summer Student
Total Posts:  8
Joined  06-27-2008

First of all, sorry about my bad english, if you have some trouble understanding something that I have said, please ask.

Second: Thanks for the library! That’s what I was searching for a long time. Now I can use file and form validation in the same controller without problems.

I’m making a new version of my web and started using it for the file uploading forms(almost all are images) and for now i dont had any problem until today.

The thing is that I have learned that if you make a bmp file in the windows paint and change the extension from bmp to jpg the form_validation thinks that it’s a jpg, and it seems that the upload library of codeigniter has the same problem. So I have made a little change in the file_allowed_type function of your library to resolve that. Now instead of using the “type” parameter of the $_FILES variable, I make a new “type” parameter from the uploaded file and, using a MIME function of the upload library I compare the new “type” instead of using file extension comparations.

function file_allowed_type($file,$type)
    
{
      
        
//is type of format a,b,c,d? -> convert to array
        
$exts explode(',',$type);
                
        
//is $type array? run self recursively
        
if(count($exts)>1)
        
{
            
foreach($exts as $v)
            
{
                $rc 
$this->file_allowed_type($file,$v);
                if(
$rc===TRUE)
                
{
                    
return TRUE;
                
}
            }
        }
        
        
//is type a group type? image, application, word_document, code, zip .... -> load proper array
        
$ext_groups = array();
        
$ext_groups['image'= array('jpg','jpeg','gif','png');
        
$ext_groups['application'= array('exe','dll','so','cgi');
        
$ext_groups['php_code'= array('php','php4','php5','inc','phtml');
        
$ext_groups['word_document'= array('rtf','doc','docx');
        
$ext_groups['compressed'= array('zip','gzip','tar','gz');
        
      
$retorno false;
      
                
//with this we get the TRUE MIME of the file, not the fake one
                //'cause upload bug
                
$ftype 'application/octet-stream';
                
$finfo = @new finfo(FILEINFO_MIME);
                
$fres = @$finfo->file($file['tmp_name']);
                if (
is_string($fres) && !empty($fres)) {
                   $tipo_archivo 
$fres;
                

                
else
                
{
                
return False;
                
$this->set_message('file_allowed_type',"Error, tipo inexistente.");
                
}
      
      
      $CI 
=& get_instance();    
      
$CI->load->library('Upload');
      
        
      
$tipo $ext_groups[$exts[0]];

            foreach(
$tipo as $extension)
            
{
                $mimes_internos  
$CI->upload->mimes_types($extension);
                if(
is_array($mimes_internos))
                
{
                        
if (in_array($tipo_archivo,$mimes_internos))
                        
{
                        $retorno 
true;
                        
}
                }
                
else
                
{
                        
if ($mimes_internos == $tipo_archivo)
                        
{
                        $retorno 
true;
                        
}
                }

            }
        
        
        
   

      
if($retorno == true)
        
{
        
return TRUE;
        
}
        
else
        
{
        $this
->set_message('file_allowed_type',"%s no puede ser del formato $tipo_archivo.");
        return  
false;   
        
}
        
    } 

Again, if you have some problem understanding me, ask whatever you want.

Profile
 
 
Posted: 08 November 2010 01:17 AM   [ Ignore ]   [ # 65 ]  
Summer Student
Total Posts:  2
Joined  11-07-2010
umefarooq - 09 February 2010 05:30 AM

nice extension i really like it but one thing i want to ask can we use it for multiple file upload or array of files uploading. for example

<input type="file" name="upload[]" />
<
input type="file" name="upload[]" />
<
input type="file" name="upload[]" /> 

will it work for it.

i had the same problem too..

i had try with few test

for ($i=0$i $num_files $i++) {
       $this
->form_validation->set_rules("userfile[$i]","File","file_required|file_size_max[2048KB]|file_size_min[10KB]|file_allowed_type[image]");
   

this is not working in the validation

for ($i=0$i $num_files $i++) {
$file_field_name[$i] 
$_FILES['userfile']['name'][$i];
       
$this->form_validation->set_rules($file_field_name[$i],"File","file_required|file_size_max[2048KB]|file_size_min[10KB]|file_allowed_type[image]");
   

this is not working too.

please point out my mistake. or the library isn’t working with multiple upload?

Profile
 
 
Posted: 22 September 2011 04:43 AM   [ Ignore ]   [ # 66 ]  
Summer Student
Avatar
Total Posts:  1
Joined  09-22-2011

can this libray used at CI 2.0.2 ? because, i try this tutorial it seem didnt work at my apps. sorry my bad english

Profile
 
 
   
7 of 7
7