Part of the EllisLab Network
   
3 of 7
3
Extended form_validation: now with file checking :)
Posted: 06 August 2009 04:38 PM   [ Ignore ]   [ # 21 ]  
Lab Assistant
RankRank
Total Posts:  107
Joined  01-23-2009

There is an issue here check this link:
http://ca3.php.net/manual/en/features.file-upload.errors.php

lookup these:
UPLOAD_ERR_INI_SIZE
UPLOAD_ERR_FORM_SIZE

As you can see if the file size is already violated then the file will not be available.

Will it be ok if I use your _let_to_num($v) in the new version of the code?

Profile
 
 
Posted: 07 August 2009 03:29 AM   [ Ignore ]   [ # 22 ]  
Grad Student
Rank
Total Posts:  61
Joined  05-27-2009

It seems to solve most of the upload issues in a simple and effective way. Please can you clarify on following points as I am new to php and CI.


1.Should I save this extension of class validation as a separate file in library ? or simply I can paste it on the ‘form_validation’ library ?


2.In view, we give set_value for text type. How to give set_value for file type ? Is it the same like text type ?

I tried it by creating a separate file and included in the library.It didn’t work.( I did include that library in controller)and I tried set_value and that too didn’t work.

Any help in this regard from any one of you is highly appreciated.

Thanks in advance.

Profile
 
 
Posted: 07 August 2009 04:14 AM   [ Ignore ]   [ # 23 ]  
Grad Student
Rank
Total Posts:  42
Joined  01-08-2009

1) You should extend the the class, don’t change the CI code as you’ll have a nightmare if you want to upgrade to the latest version.

2) You can’t set the value of a file field, due to security reasons (e.g. you could set it to a file they didn’t specify and grab it from their system!).

 Signature 

Web Design Essex - Essex SEO

Profile
 
 
Posted: 07 August 2009 06:22 AM   [ Ignore ]   [ # 24 ]  
Grad Student
Avatar
Rank
Total Posts:  43
Joined  07-06-2008

Will it be ok if I use your _let_to_num($v) in the new version of the code?

Of course, but you might be even more interested in this one:

function let_to_bit($sValue)
  
{
    
// Split value from name
    
if(!preg_match('/([0-9]+)([ptgmkb]{1,2}|)/ui',$sValue,$aMatches))
    
{ // Invalid input
      
return FALSE;
    
};
    if(empty(
$aMatches[2]))
    
{ // No name -> Enter default value
      
$aMatches[2] = 'KB';
    
};
    if(
strlen($aMatches[2]) == 1)
    
{ // Shorted name -> full name
      
$aMatches[2] .= 'B';
    
}
    $iBit   
= (substr($aMatches[2], -1) == 'B') ? 1024 : 1000;
    
// Calculate bits:
    
switch(strtoupper(substr($aMatches[2],0,1))){
      
case 'P':
        
$aMatches[1] *= $iBit;
      case
'T':
        
$aMatches[1] *= $iBit;
      case
'G':
        
$aMatches[1] *= $iBit;
      case
'M':
        
$aMatches[1] *= $iBit;
      case
'K':
        
$aMatches[1] *= $iBit;
        break;
    
}

    
// Return the value in bits
    
return $aMatches[1];
  
}

  
// The following lines were used to test the above function:
  
echo '1 '.let_to_bit('')    . '<br />'; // returns FALSE
  
echo '2 '.let_to_bit('2')   . '<br />'; // returns 2048
  
echo '3 '.let_to_bit('2K')  . '<br />'; // returns 2048
  
echo '4 '.let_to_bit('2L')  . '<br />'; // returns 2048
  
echo '5 '.let_to_bit('2Kb') . '<br />'; // returns 2000
  
echo '6 '.let_to_bit('2KB') . '<br />'; // returns 2048

I wrote the function for your extension, so please feel free to use it if you like.

Profile
 
 
Posted: 07 August 2009 06:59 AM   [ Ignore ]   [ # 25 ]  
Lab Assistant
RankRank
Total Posts:  107
Joined  01-23-2009
quest13 - 07 August 2009 03:29 AM

2.In view, we give set_value for text type. How to give set_value for file type ? Is it the same like text type ?

To approach this and reduce the headache of uploading files on each form_validation failure you can do this:

1. if file had no problem copy it to temp folder and keep the link to it in session.
2. on your form if the link is specified then make the field optional.

Profile
 
 
Posted: 08 August 2009 04:42 AM   [ Ignore ]   [ # 26 ]  
Grad Student
Rank
Total Posts:  61
Joined  05-27-2009

I was away from code igniter forum for the past one month as I was busy doing a project in wordpress. I found a tremendous difference between CI and wordpress or any other framework as a matter of fact,in terms of forum response and technical bench strength. The response from CI is just overwhelming.I sometimes wonder whether someone is already started replying my question when I just start typing in the forum ! The response is so quick and so prompt.
I have become a die-hard fan of CI now.

Thanks once again for my file upload validation query. But I still have one problem.In my view page, I have only “name” and “file” mentioned, not the size. But in file_required function, it checks for the ‘size’(===0).It means I have to tell about the size also in my view page as a mandatory requirement ?

Thanks

Profile
 
 
Posted: 08 August 2009 05:10 AM   [ Ignore ]   [ # 27 ]  
Grad Student
Rank
Total Posts:  42
Joined  01-08-2009

The size is handled automatically, all you need is the file field in you view. PHP will then generate a global variable with all the details of the uploaded file(s), ‘size’ is one of the attributes.

See http://us3.php.net/manual/en/reserved.variables.files.php for more information.

 Signature 

Web Design Essex - Essex SEO

Profile
 
 
Posted: 08 August 2009 11:55 AM   [ Ignore ]   [ # 28 ]  
Lab Assistant
RankRank
Total Posts:  107
Joined  01-23-2009

For Those of you that like this class and are using it I have a news smile
The new version of the code is now completed. you can check it out at:
http://devbro.com/testing/ci_form_validation/

I have added a few new rules:
file_min_size
file_disallowed_type
file_image_mindim
file_image_maxdim

let me know what you think of the new code.

Profile
 
 
Posted: 09 August 2009 03:46 PM   [ Ignore ]   [ # 29 ]  
Grad Student
Avatar
Rank
Total Posts:  43
Joined  07-06-2008

Great work Devbro. I’m Looking forward to upgrade to version 2. :p

Profile
 
 
Posted: 13 August 2009 04:45 AM   [ Ignore ]   [ # 30 ]  
Grad Student
Rank
Total Posts:  61
Joined  05-27-2009

I am sorry to repeat.But my problem is not solved.I will write down step by step,please someone correct me where am I committing the mistake.

1. I downloaded it from “http://devbro.com/testing/ci_form_validation/”.

2. I created a file called “MY_Form_validation” and saved it in the libraries folder where the “Form_validation.php” is kept.

3. I included this library in my controller like this

$this->load->library('MY_Form_validation');

4. My controller looks like this,

$this->form_validation->set_rules("uploadedfile","File Upload","file_required|file_min_size[10KB]|file_max_size[500KB]|file_allowed_type[image]|file_image_mindim[50,50]|file_image_maxdim[400,300]");


5. My view look like this,

<tr>
    <
td><input type="hidden" name="MAX_FILE_SIZE" value="100000" /><label>
    
Select an Image to upload:</label></td>

    <
td><input name="uploadedfile" type="file"
       
<?php echo set_value('uploadedfile'); ?> id="uploadedfile"
    
/></td>
    </
tr>

    <
tr><td></td>
    <
div class="error"><?php echo form_error('uploadedfile'); ?></div>
    </
tr>

 

But when I submit my form without loading any file, it doesnot falling into

if ($this->form_validation->run() == FALSE){
$this
->Addimage($productid);
}else {

$data[
'result']=$this->product_model->uploadimage($productid);

}

it goes to else part instead.


Where am I doing the mistake ?


Any help regarding this will be extremely helpful to me as it will reduce many lines of unnecessary code.

Thanks.

Profile
 
 
   
3 of 7
3
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 120209 Total Logged-in Users: 75
Total Topics: 126341 Total Anonymous Users: 6
Total Replies: 664431 Total Guests: 506
Total Posts: 790772    
Members ( View Memberlist )