Validation of multiple file uploads

Let's write validation for multiple file uploads.

HTML side

This is a file upload form on the HTML side. There are four file upload fields. It corresponds to error display and deletion.

<form action = <%= url_for%> method = "POST", enctype = "multipart / form-data">
  %= hidden_field op =>'update';
  %= hidden_field id => $id;
  <ul>
    <li>
      Image 1. <%= file_field'image1'%>
      <div class = "error"> <%= $errors->{'image1'}? $errors->{'image1'}:''%> </div>
      Delete <%= check_box'image1_delete'%>
    </li>
    <li>
      Image 2. <%= file_field'image2'%>
      <div class = "error"> <%= $errors->{'image2'}? $errors->{'image2'}:''%> </div>
      Delete <%= check_box'image2_delete'%>
    </li>
    <li>
      Image 3. <%= file_field'image3'%>
      <div class = "error"> <%= $errors->{'image3'}? $errors->{'image3'}:''%> </div>
      Delete <%= check_box'image3_delete'%>
    </li>
    <li>
      Image 4. <%= file_field'image4'%>
      <div class = "error"> <%= $errors->{'image4'}? $errors->{'image4'}:''%> </div>
      Delete <%= check_box'image4_delete'%>
    </li>
  </ul>
</form>

Program side

This is a sample code on the program side. What you get with the parameters will be a Mojo::Upload object. The image information is created by taking the values ​​of multiple file fields and used as array reference.

If there is a file size, the file type is determined.

<%
  my $op = param ('op') //'';
  my $id = param ('id');

  my $errors = {};
  if ($self->req->methodeq'POST') {
    if ($op eq'update') {

      #Get parameters
      my $images = [];
      my $image_count = 4;
      for (my $number = 1; $number <= $image_count; $number ++) {
        
        my $image_upload = param ("image $number");
        
        my $image = {
          upload => $image_upload,
          number => $number,
        };;
        
        push @$images, $image;
      }

      #Validation
      for my $image (@$images) {
        my $image_upload = $image->{upload};
        
        if ($image_upload->size> 0) {
          #Content type
          my $image_content_type = $image_upload->headers->content_type;
          
          # extension
          my $ext;

          #PNG
          if ($image_content_type = ~ m | \ bimage / png \ b |) {
            $ext ='png';
          }
          #JPEG
          elsif ($image_content_type = ~ m | \ bimage / jpeg \ b |) {
            $ext ='jpg';
          }
          #GIF
          elsif ($image_content_type = ~ m | \ bimage / gif \ b |) {
            $ext ='gif';
          }
          #PDF
          elsif ($image_content_type = ~ m | \ bapplication / pdf \ b |) {
            $ext ='pdf';
          }
          
          unless (defined $ext) {
            my $number = $image->{number};
            $errors->{"image $number"} = "The file type of image $image->{number} is invalid.";
          }
        }
      }
    }
  }
}
%>

Associated Information