#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2008
    Posts
    152
    Rep Power
    0

    RegEx to upload pdf files that contain only letters (


    I'm trying to force the upload script to accept pdf files that contain only letters (lower case and upper case) and numbers with the extension PDF or pdf and I just can't seem to get my RegEx pattern right. Your help would be greatly appreciated.

    PHP Code:
    if(isset($_POST['upload'])) 
            {
            
                        
    $regpattern '/[A-Za-z0-9].pdf/';
                        
    $name $_FILES['userfile']['name'];
                        
    $tmpName $_FILES['userfile']['tmp_name'];
                        
    $size $_FILES['userfile']['size'];
                        
    $type $_FILES['userfile']['type'];
                        
                        
                        
                        
                        
                        if (
    preg_match($regpattern$name)) {
                                echo 
    $name .' ' "MATCH";
                                } else {
                                        echo 
    $name ' ' "NO MATCH.";
                                        }
                                } 

    Below is the HTMLfor the form
    Code:
    <form action="" method="post" enctype="multipart/form-data" id="form1">
    <table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
    <tr>
    <td width="246">
    <input name="userfile" type="file" id="userfile"/></td>
    <td width="80">
    <input name="upload" type="submit" id="upload" value="Upload" />
    </td>
    </tr>
    </table>
    
    </form>
  2. #2
  3. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    [A-Za-z0-9] matches a single character. You need to use a repetition specifier if you want to allow the set to match multiple characters.

    For example, [A-Za-z0-9]+ means one or more.

    . has special meaning in a regular expression, it means any character. Therefore you need to escape the character in your regular expression using a backslash.

    If you want to match the entire filename you need starting and ending anchors on the regular expression, otherwise the expression can evaluate to true if it is found in the middle of the filename. ^ represents the start of the string, $ represents the end of it.

    So your final regular expression should look something like this:
    /^[A-Za-z0-9]+\\.pdf$/

    Although note that this doesn't handle both pdf and PDF endings.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2011
    Posts
    30
    Rep Power
    3
    Originally Posted by E-Oreo
    So your final regular expression should look something like this:
    /^[A-Za-z0-9]+\\.pdf$/

    Although note that this doesn't handle both pdf and PDF endings.
    to make it handle PDF and pdf and Pdf, just add the 'i' modifier
    "/^[A-Za-z0-9]+\\.pdf$/i"

IMN logo majestic logo threadwatch logo seochat tools logo