SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    Confirmed Halfwit
    Join Date
    Oct 1999
    Location
    Vancouver, BC, Canada
    Posts
    983
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to "filter" a certain file extension when uploading?

    I have an upload form, and I'd like to do some verification before submitting it. Basically, I want to check if the inputted filename has a valid extension type.

    My problem is that I don't know how to extract the extension from the inputted file name.

    So let's say my filename is "C:\path\t.o\the\b.ig\fi.le.gif"

    How can I extract the extension? (I get messed up if there are multiple dots in the name or directories.)

    Thanks!
    - A simple online WYSIWYG editor for HTML code snippets.
    - Managed Web Hosting - $3.95/month (resellers welcome)
    - Why pay more? $8.95 domains & $9.95 SSL certificates!

  2. #2
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    use a split and pop?

    var ext=(object.location.href.split(/./)).pop();

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  3. #3
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    check out this thread:

    http://www.sitepointforums.com/showt...threadid=53926

    along these lines, you can also just do a string comparison on the last 3 letters:

    var sString;
    var iPathExtension;
    var sExtension;

    sString = frmYourForm.txtYourTextBox.value;
    iPathExtension = (sString.length) - 3;
    sExtension = sString.substring(iPathLength);

    if(sExtension.toLowerCase() != "txt")
    alert("This is not a text file.");

  4. #4
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ummm.. i'm not being stupid... am i... or didn't i post a ONE line command that gives you the file exstension?


    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  5. #5
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Flawless_koder,
    Nice to see someone else who likes minimalist solutions!!!! GWF!


    M@rco

  6. #6
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    GWF <<< ????!

    Minimilist i do... Mindreading i find a bit harder.

    he he

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  7. #7
    ********* obeah makeda's Avatar
    Join Date
    Jun 2001
    Location
    rollin' on dubs
    Posts
    492
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ummm.. i'm not being stupid... am i... or didn't i post a ONE line command that gives you the file exstension?
    Yes you did but your syntax was wrong.

    should be:

    var ext=(object.location.href.split(".")).pop();
    Last edited by makeda; Mar 25, 2002 at 13:13.

  8. #8
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <SMUGMODE>
    Actually, his syntax is perfectly fine. The split() method can take either a string or a regular expression, of which /./ is the latter!
    </SMUGMODE>



    M@rco

  9. #9
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well actually... split is part of Javascript's regex subsys... so it's a miracle it supports strings. Regex search operators are fairly flexible though.


    i'm also not sure you need the () around the obj.split which creates the array since the last element is actually the creator (however at the same time Javascript is sometimes fussy about having split(). )

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  10. #10
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You lost me with:
    split is part of Javascript's regex subsys... so it's a miracle it supports strings. Regex search operators are fairly flexible though.
    But you're quite right about the brackets! Clearly this is fine:
    Code:
    var ext=object.location.href.split(/./).pop();

    M@rco

    PS - "GWF" --> Good Work Fella (It's probably not 'official' Internet shorthand, but comes from Loaded magazine, or possibly FHM, I can't remember which)
    Last edited by M@rco; Mar 26, 2002 at 04:28.

  11. #11
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thought it might be.

    It even work if you're pulling the obj from the os.

    For instance an activeX FO shell returner:

    Code:
    var fo=new ActiveXObject("Scripting.FileSystemObject");
    var ff=fo.GetFile("C:\\windows\system32\drivers\etc\hosts");
    var fr=ff.openAsTextStream( forReading, 20 );
    
    var stratii=new Array();
    while (!fr.AtEndOfStream){
           var ext=fr.ReadLine().split(/]/).pop();
           var stratus=fr.ReadLine().split(/\s/).shift();
           stratii[stratus]=ext;
           }
    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  12. #12
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't get your point - the split() method can be used for any string! That's just another example! I'm sure there's 1000 more where that came from! lol


    M@rco

  13. #13
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope... i wasn't sure it would work on a method that's returning from an object with a virtual classid outside it's domain of influence.

    Flawless
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  14. #14
    The doctor is in... silver trophy MarcusJT's Avatar
    Join Date
    Jan 2002
    Location
    London
    Posts
    3,509
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Right, I get you. Glad we cleared that up!!!


    M@rco

    PS: ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
    Last edited by M@rco; Mar 31, 2002 at 08:56.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •