Simple function to validate for .pdf extension?

Discussion in 'PHP' started by dwest, Dec 27, 2006.

  1. #1
    Hi,
    My apologies for such a rudimentary request but I am a stone cold newbie at how best to check strings for certain conditions.

    I have a file upload form with a file element so the user can select the file they wish to upload.

    I need to isolate the file extension of whatever file they selected and determine if it is ".PDF" regardless of how long the path is to the file.

    How do I do that?
    An example path might be:
    C:\Program Files\xampp\htdocs\wordpress\wp-content\test_file.pdf


    Many thanks!
     
    dwest, Dec 27, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    if (strtolower(substr(strrchr($file, '.'), 1)) == 'pdf')
    {
         // Do something
    }
    
    
    PHP:
     
    nico_swd, Dec 27, 2006 IP
  3. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks!
    Where can I find explanations of what these commands are doing so I can learn?
    Also, I realized I need to validate that the field isn't empty as well.
    How would I accomplish that?
     
    dwest, Dec 27, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    strtolower() - Converts the string to lowercase
    substr() - Pops the last character from the string (in this case)
    strrchr() - Find the last occurrence of a character in a string.

    Click on the functions in my script. It's all explained there as well. ;)

    My script checks if the extension is "pdf", so it returns false when it's not, or when the filename is empty. It should work the way it is.
     
    nico_swd, Dec 27, 2006 IP
  5. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Many Thanks!
     
    dwest, Dec 27, 2006 IP
  6. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Hi,
    It doesn't seem to be working. All I get is the error message regardless of the file extension.

    $pdfu_file = $_REQUEST['pdfu_file'];
          if (strtolower(substr(strrchr($pdfu_file, '.'), 1)) == 'pdf')
              {
                 pdfupload_process_upload();
                    }else{
                      echo("The file must be a .pdf file.");
                           }
            }
    Code (markup):
     
    dwest, Dec 27, 2006 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    What do you get echoing $_REQUEST['pdfu_file']?
     
    nico_swd, Dec 27, 2006 IP
  8. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    er...um...nothing.
    What's up with that :)
     
    dwest, Dec 27, 2006 IP
  9. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #9
    Is this file supposed to be a user uploaded file? If so you'll need the $_FILES variable. And post your HTML form.
     
    nico_swd, Dec 27, 2006 IP
  10. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Here's the form
    <form name="pdfu_form" action="" method="POST" enctype="multipart/form-data">
     <div><input type="hidden" name="formtrigger" value="uploaded" /></div>
     <div><strong>Select a PDF file for uploading:</strong></div>
     <div><input name="pdfu_file" type="file" size="45" tabindex="1" />
     </div>
     <div>After you successfully upload your file, the notification form will become available. You can test your upload by click the link in the success message.</div>
     <div><input type="submit" name="pdfu_btn" value="Upload" tabindex="2" /></div>
     </form>
    Code (markup):
    The user is selecting a file from his local computer to upload to a folder on the
    webserver.

    HTH.
     
    dwest, Dec 27, 2006 IP
  11. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #11
    Try $_FILES['pdfu_file']['name'] instead. Seems like you're using a premade script though... maybe you can set the allowed extensions there at some place.
     
    nico_swd, Dec 27, 2006 IP
  12. dwest

    dwest Peon

    Messages:
    38
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Got it!
    You were correct. The $FILES variable was needed.
    Thanks so much for the assist.
    I'm learning...I'm learning :)
     
    dwest, Dec 27, 2006 IP
  13. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #13
    
    if($_FILES['fieldname']['type'] == 'application/pdf){
    echo 'File is PDF';
    }
    
    PHP:
    * Not sure of the "application/pdf".

    Remember that uploaded files can have fake extensions.

    Peace,
     
    Barti1987, Dec 27, 2006 IP
  14. technoguy

    technoguy Notable Member

    Messages:
    4,369
    Likes Received:
    306
    Best Answers:
    0
    Trophy Points:
    205
    #14
    right azizny

    But yours one is the best solution i think because windows can have file name like this too

    abc.pqr.xyz.pdf so we should consider last index of ".".

    But yours one is the best one
     
    technoguy, Dec 27, 2006 IP