2

I'm using the following CodeIgniter function to upload files which works fine:

function uploadFiles(){

         $this->load->library('upload');
         $error = 0;    
         $projectName = $_POST['projectname'];
         $projectID = $_POST['maxid'];
         $folderName = $this->config->item('upload_dest')."/".$projectName."_".$projectID;
         if(!file_exists ($folderName)){
             $aa = mkdir($folderName);
         }

         $config['upload_path']   = $folderName;
         $config['allowed_types'] = 'xml';
         //$config['allowed_types'] = '*';
         $config['max_size']      = '0';
         $config['overwrite']     = TRUE;

         $this->upload->initialize($config);

         for($i=0; $i<count($_FILES['files']['name']); $i++)
         {

           $_FILES['userfile']['name']    = $_FILES['files']['name'][$i];
           $_FILES['userfile']['type']    = $_FILES['files']['type'][$i];
           $_FILES['userfile']['tmp_name'] = $_FILES['files']['tmp_name'][$i];
           $_FILES['userfile']['error']       = $_FILES['files']['error'][$i];
           $_FILES['userfile']['size']    = $_FILES['files']['size'][$i];

              if($this->upload->do_upload())
              {
                $error += 0;
              }else{
                $error += 1;
              }
         }

         if($error > 0){ 
            $this->upload->display_errors();
            return FALSE; 
         }
         else{ 
            return TRUE; 
         }

    }

What I need to do is - check to make sure that at least one of the files which are being uploaded is named "etl". If there's no such a file in the file list the user chosen - stop the action, don't upload anything and return a form validation error. Could anybody advise over this?

Thanks.

2 Answers 2

1

Firstly, from php there is no way to get the name of the file(s) before uploading, you must upload to get the properties of the file. So, the options available are:

(1) Allow the files to be uploaded, then get the names and check if any contains "etl". If non contains what you are looking for, then delete the just uploaded files, and set a custom error message yourself. This approach have a very large overhead cost of allowing you to first upload what is not need, then deleting it. Very poor but solves the problem.

(2) On the otherhand, is the javascript solution. Give the upload fields a common class name e.g "userfile1", "userfile2", .......

then from your javascript and using jquery, intercept the submission of the form, then use a for loop to get the values of each of the file upload field, from which you can get the full name and extension of the file and then do your "etl" comparison.

i.e

<script type="text/javascript" >
  $("#formname").submit(function(){
    $(".classname").each(function(){
      if($(this).val().indexOf("etl") != -1 ){
        return true;
      }
    });
    /*
     *whatever makes it finish executing that loop and the execution of code gets 
     *to this point, then the string "etl" was not found in any of the names.
     */
     // write a piece of code to show an hidden error field
     $("#hidden_error_div").text("Your error message").show();
     return false;  //makes sure the form is not submitted.
  });
</script>

Hope this helps.

Sign up to request clarification or add additional context in comments.

1 Comment

The accepted answer on this question ( stackoverflow.com/questions/9277247/… ) explains a way to check if a file was selected in the upload field before calling do_upload() ... using PHP.
0

Oyekunmi gives a good javascript solution to intercept before it actually gets to the server. As Oyekunmi points out, once it gets there, it gets there as a package, so you could then store and process it in a temporary directory, eval each file there and process accordingly.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.