1
votes

I am trying to validate all of my uploaded files, actually i do file size and file type validation. If validation fails for any file, i record that particular file for error message.I am using for each loop for file processing and if condition for file size and file type validation, but the problem is only first if condition is evaluated and second if block is not tested. Both if lies inside for each loop. Any help will be appreciated...

code

 public function validate_my_files(){

     foreach($this->files_from_user_validateit['name'] as $key => $value){
        $name = $this->files_from_user_validateit['name'][$key];
        $size = $this->files_from_user_validateit['size'][$key];
        $type = $this->files_from_user_validateit['type'][$key];
        //$error[$key] = $this->files_from_user_validateit['error'][$key];
        //$tmp_name[$key] = $this->files_from_user_validateit['tmp_name'][$key];

        //not allowed file extensions
        $not_allowed_ext = array('php','js','asp','aspx','py','c','c++','jsp','perl','cfg','css','java',
                                  'json');
        //lets find the extension of the filename
        $est = explode('.',$name);
        $ext = $est[count($est)-1];
        if(in_array($ext,$not_allowed_ext)){
          //putting in session for later message
          $_SESSION['ext'][] = $name;
        }

        //lets check for file size now, lets not allow file greater than 50MB 
        if($size > 47185920){
          $_SESSION['size'][] = $name;
        }
     }
     print_r($_SESSION);die();
   }

only file extension if is tested whereas if condition for size do not works...

EDIT

Unfortunately there is huge problem with my upload form. Files selected at later is only processed by $_FILES, I am using multiple file upload and displaying selected files on the table in the same page using java script. say i select 5 files at first (those 5 files displayed on the table) and again select 3 files,now total there are 8 files on the table.But only last 3 files that i selected 2nd time is available in $_FILES array. Please anybody have solution for this...

RE-EDIT

Id 'myfiles' is for holding multiple files from input file field. Id 'dataS' is div that table created by below given JS. IF 'tot' is a span that total size of file on the table. INPUT FORM:

   <form id="uploadform" method="post" action="" enctype="multipart/form-data">
        <div id="datas"></div>

        <div id="tot"><h3>TOTAL SIZE:</h3> <span id="zise">0 MB</span></div>

        <div id="addfiles"><input type="file" name="addfiles[]" value="Add Files" multiple="multiple" id="myfiles" ></div>

         <input type="submit" name="sbmtfile" style="color:green;font-size: 17px;">

    </form>

My JS:

<script type="text/javascript">
   var s = 1;
   var totsize = 0;

   myfiles.onchange = function(){
      var a = document.getElementById('myfiles').files;
      var b = document.getElementById('datas');
      var total = document.getElementById('tot');//for table title

    var c = document.createElement('TABLE');
    c.className = 'showtable';
    c.border = '1px';
    c.cellspacing = '0';
    c.cellpadding = '2';
    c.width = '100%'

    for(var i = 0; i < a.length; i++){
        var f = document.createElement('TR');
        c.appendChild(f);
        //for total file size
        totsize = totsize + a[i].size;

        var r = document.createElement('TD');
        r.align = 'center';
        r.width = '152px';
        f.appendChild(r);
        r.appendChild(document.createTextNode(s++));

        var y = document.createElement('TD');
        y.align = 'left';
        y.width = '610px';
        y.appendChild(document.createTextNode(a[i].name));
        f.appendChild(y);

        var z = document.createElement('TD');
        z.align = 'center';
        r.width = '152px';
        z.appendChild(document.createTextNode(Math.round((a[i].size)/1024)));
        f.appendChild(z);

        var x = document.createElement('TD');
        x.align = 'center';
        x.width = '152px';
        x.appendChild(document.createTextNode('DELETE'));
        f.appendChild(x);
    }

    b.appendChild(c);
    document.getElementById('zise').innerHTML = Math.round((totsize/(1024*1024)))+ ' MB';
 }

</script>
2
is files_from_user_validateit() a custom function? Since you are checking if($size > 47185920), which is basically if($this->files_from_user_validateit['size'][$key]; > 47185920), you need to check what the value it returns. - Sean
@Sean ($this->files_from_user_validateit) holds all of my uploaded files, that i get from form, i.e. it is an array of files and it is working fine, i can get all of my uploaded files here... - mindre

2 Answers

0
votes

Are you looking for the else language element?

if (in_array($ext,$not_allowed_ext)) {
          //putting in session for later message
          $_SESSION['ext'][] = $name;
} else {
  ####
    //lets check for file size now, lets not allow file greater than 50MB 
    if ($size > 47185920) {
        $_SESSION['size'][] = $name;
    }
}

Only check for size if not an extension violation.

(not a beautiful example I made this, and there is also else if, but in case you didn't know of else, I think it's more important to see else first)

0
votes

As you said our first if condition is working fine but not working for rest so i thing the issue in for loop.So just check this i thing it will help

public function validate_my_files(){

 foreach($this->files_from_user_validateit as $key => $value){
    $name = $value['name'][$key];
    $size = $value['size'][$key];
    $type = $value['type'][$key];
    //$error[$key] = $this->files_from_user_validateit['error'][$key];
    //$tmp_name[$key] = $this->files_from_user_validateit['tmp_name'][$key];

    //not allowed file extensions
    $not_allowed_ext = array('php','js','asp','aspx','py','c','c++','jsp','perl','cfg','css','java',
                              'json');
    //lets find the extension of the filename
    $est = explode('.',$name);
    $ext = $est[count($est)-1];
    if(in_array($ext,$not_allowed_ext)){
      //putting in session for later message
      $_SESSION['ext'][] = $name;
    }

    //lets check for file size now, lets not allow file greater than 50MB 
    if($size > 47185920){
      $_SESSION['size'][] = $name;
    }
 }
 print_r($_SESSION);die();

} Just change

$this->files_from_user_validateit['name']

With

$this->files_from_user_validateit

And

$this->files_from_user_validateit['name'][$key]

with

$value['name'][$key]

It may help you.