Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:48:57+00:00 2026-06-15T10:48:57+00:00

How do I determine whether a given string is a valid Windows filename? I’m

  • 0

How do I determine whether a given string is a valid Windows filename? I’m thinking of some function that I can give a string and that returns a boolean. It should check for disallowed characters (<>:”/\|?*) and for reserved words (CON, PRN, et cetera).

isValidWindowsFilename('readme.txt'); // true
isValidWindowsFilename('foo/bar'); // false
isValidWindowsFilename('CON'); // false

I’ve found a MSDN reference describing exactly what is valid: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx

I’ve also found the same question for Java, with an answer that does what I need, except that it’s Java and not PHP: Validate a file name on Windows

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:48:58+00:00Added an answer on June 15, 2026 at 10:48 am

    Here’s the answer on the Java question, ported to PHP.

    /**
     * @param string $filename
     * @return boolean Whether the string is a valid Windows filename.
     */
    function isValidWindowsFilename($filename) {
        $regex = <<<'EOREGEX'
    ~                               # start of regular expression
    ^                               # Anchor to start of string.
    (?!                             # Assert filename is not: CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
        (CON|PRN|AUX|NUL|COM[1-9]|LPT[1-9])
        (\.[^.]*)?                  # followed by optional extension
        $                           # and end of string
    )                               # End negative lookahead assertion.
    [^<>:"/\\|?*\x00-\x1F]*         # Zero or more valid filename chars.
    [^<>:"/\\|?*\x00-\x1F\ .]       # Last char is not a space or dot.
    $                               # Anchor to end of string.
                                    #
                                    # tilde = end of regular expression.
                                    # i = pattern modifier PCRE_CASELESS. Make the match case insensitive.
                                    # x = pattern modifier PCRE_EXTENDED. Allows these comments inside the regex.
                                    # D = pattern modifier PCRE_DOLLAR_ENDONLY. A dollar should not match a newline if it is the final character.
    ~ixD
    EOREGEX;
    
        return preg_match($regex, $filename) === 1;
    }
    

    Note that this function does not impose any limit on the length of the filename, but a real filename may be limited to 260 or 32767 chars depending on the platform.

    Here’s some test code to verify its correctness.

    $tests = array(
        // Valid filenames
        'readme.txt' => true,
        'foo.AUX' => true,
        'foo.AUX.txt' => true,
        '.gitignore' => true, // starting with a period is ok.
    
        // Invalid filenames
        'x<y' => false, // less than not allowed.
        'x>y' => false, // greater than not allowed.
        'q: why not.txt' => false, // colon not allowed.
        'He said "hi".doc' => false, // double quote not allowed.
        'foo/bar' => false, // Forward slash not allowed.
        'foo\\bar' => false, // Backslash not allowed.
        'cat readme | backup' => false, // vertical bar (pipe) not allowed.
        'ls foo?.rtf' => false, // question mark not allowed
        'ls foo*.rtf' => false, // asterisk not allowed
        "null\0char" => false, // null character not allowed
        '.' => false, // must not end in period
        '..' => false, // must not end in period
        'period.' => false, // must not end in period
        'space ' => false, // must not end with space
    
        // Do not use the following reserved device names for the name of a file:
        // CON, PRN, AUX, NUL, COM1, COM2, COM3, COM4, COM5, COM6, COM7, COM8, COM9, LPT1, LPT2, LPT3, LPT4, LPT5, LPT6, LPT7, LPT8, and LPT9.
        // Also avoid these names followed immediately by an extension; for example, NUL.txt is not recommended.
        'CON' => false,
        'prn.txt' => false,
        'LPT9.php' => false,
    );
    
    // Disallow characters whose integer representations are in the range from 1 through 31
    for ($i = 1; $i <= 31; $i++) {
        $tests[chr($i)] = false;
    }
    ?>
    
    <style>
    .pass {
        background-color: #efe;
    }
    .fail {
        background-color: #fee;
    }
    </style>
    
    <table>
    <thead><tr><th>filename</th><th>expected</th><th>actual</th></tr></thead>
    <tbody>
    <? foreach ($tests as $filename => $expected) {
        $actual = isValidWindowsFilename($filename);
        ?>
        <tr><td><?=htmlentities($filename)?></td><td><?= $expected ? 'valid' : 'invalid' ?></td><td class="<?= $expected === $actual ? 'pass' : 'fail' ?>"><?= $actual ? 'valid' : 'invalid' ?></td></tr>
        <?
    } ?>
    </tbody>
    </table>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an ActionScript Function object, is there any way to determine whether that function
I am using the following function to determine whether input string is valid date
If you have a given vertex how could you determine whether that vertex is
There are two polygons given. how can one determine whether one polygon is inside,
Given an Youtube video id how can I determine from JavaScript whether the video
Example: /** * This function will determine whether or not one string starts with
I am trying to determine whether a string input by a user is valid
Consider a JavaScript method that needs to check whether a given string is in
Using PHP, given a URL, how can I determine whether it is an image?
I am trying to determine whether a string contains exactly one given distinct character,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.