Learn from sources
       Member UPLOADVAL in CGIDEV2 / QRPGLESRC

       *=========================================================================
       *  RPG ILE PROGRAM CGIDEV2/UPLOADVAL
       *
       *  Sample validation program for file upload
       *
       *  This program demonstrates the ability to validate file uploads,
       *  by allowing to upload only files with extension CSV.
       *
       *  To enable this validation program:
       *  - enter command CGIDEV2/UPDEXITP (Update Exit Points)
       *  - use option 2=Change for exit point FILE-UPLOAD-001 and press Enter
       *  - in field named "User program" type UPLOADVAL
       *  - in field named "Library" type CGIDEV2
       *  - press Enter to perform the update
       *  - press F3 to exit
       *
       *  Create the program as follow:
       *  CRTBNDRPG CGIDEV2/UPLOADVAL DFTACTGRP(*NO) ACTGRP(*CALLER) DBGVIEW(*SOURCE)
       *
       *=========================================================================
       /copy CGIDEV2/qrpglesrc,hspecs
       /copy CGIDEV2/qrpglesrc,hspecsbnd
       /copy CGIDEV2/qrpglesrc,prototypeb
       /copy CGIDEV2/qrpglesrc,usec
       /copy CGIDEV2/qrpglesrc,variables3
      D UPLOADVAL       pr
      D  filename                   1024    varying
      D  retcode                      10i 0
      D UPLOADVAL       pi
      D  filename                   1024    varying
      D  retcode                      10i 0
      D getExtension    pr
      D getPath         pr
      D ThisSubProc     c                   'FILE-UPLOAD-001: '
      D returnCode      s             10i 0
      D filename1       s           1024    varying
      D s               s             20i 0
       *
      D path            s           1024    varying
      D stmfName        s           1024    varying
      D extension       s             10    varying
      D userName        s             30    varying
       /free
 
           //retrieve the user name (if any)
             username=getenv('REMOTE_USER':qusec);
           //retrieve the extension (ex. CSV) into variable "extension"
             getExtension();
           //retrieve the path into variable "path"
           //retrieve the stream file name (without extension) into variable "stmfName"
             getPath();
 
           //perform validation on the extension
             returnCode=0;           //passed
             if extension<>'CSV';
                returnCode=-1;       //not passed
             endif;
 
           // back to caller
             retcode=returnCode;
             return;
       /end-free
       *=========================================================================
       * Get the extension
      P getExtension    b
      D getExtension    pi
       /free
             clear extension;
             filename1=filename;
             s=%len(filename);
             dow s>0;
                 if %subst(filename:s:1) = '.' and
                    s+1<=%len(filename);
                    filename1=%subst(filename:1:s-1);
                    extension=%subst(filename:s+1);
                    leave;
                 endif;
                 s=s-1;
             enddo;
             extension=uppify(extension);
       /end-free
      P getExtension    e
       *=========================================================================
       * Get the path and the stream file name
      P getPath         b
      D getPath         pi
       /free
             s=%len(filename1);
             dow s>0;
                 if %subst(filename1:s:1) = '/' and
                    s+1<=%len(filename1);
                    path=%subst(filename:1:s-1);
                    stmfName=%subst(filename1:s+1);
                    leave;
                 endif;
                 s=s-1;
             enddo;
             path=uppify(path);
             stmfName=uppify(stmfName);
       /end-free
      P getPath         e
0.015 sec.s