I am going to validate the file name (either on UNIX, Linux or Windows) which the user input from the HTML textfield element and sent to .php using POST method. However, I have no idea how to check whether it validates or not. Would someone help me on this problem? Thanks a lot.

    First you have kno which characters are allowed in a filename. In windows a filename cannot have any of \/*😐?><.

    so a sample code will be:

    <?php 
    function chkFileName($filename) {
     return (strlen($filename) == strcspn($filename,'\\/*?:<>|')); 
    }
    
    // remember to escape \ with \\ before passing it to the function.
    // cuz a \ at the end of the filename will cause parse errors.
    
    if (chkFileName('valid_filename')) {
     echo 'Filename is valid';
    } else {
     echo 'Filename is not valid';
     }
    ?>
      Write a Reply...