Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Feb 2011
    Posts
    13
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Loading file names in file manager does not work with apostrophes

    So I have a file manager (similar to HFS if any of you have used it) .... Anyway when submitting ajax functions for various file actions, it doesn't work if the filenames have an apostrophe (') in them. It submits the whole part of the name up until where there's an apostrophe.

    Typically to submit an ajax function it just runs something like

    Code:
            $.post("?mode=section&id=ajax.delete", {
                filename: selectedFilesAsStr(),
            });
    Here's the javascript...

    Code:
    function selectedItems() { return $('#files .selector:checked') }
    
    function selectedFilesAsStr() {
        var a = [];
        selectedItems().each(function(){
            a.push(getItemName(this));
        });
        return a.join(":");
    }//selectedFilesAsStr
    
    function getItemName(el) {
        if (typeof el == 'undefined')
            return false;
        // we handle elements, not jquery sets  
        if (el.jquery)
            if (el.size())
                el = el[0];
            else
                return false;
        // take the url, and ignore any #anchor part
        var s = el.getAttribute('href') || el.getAttribute('value');
        s = s.split('#')[0];
        // remove protocol and hostname
        var i = s.indexOf('://');
        if (i > 0)
            s = s.slice(s.indexOf('/',i+3));
        // current folder is specified. Remove it.
        if (s.indexOf(HFS.folder) == 0)
            s = s.slice(HFS.folder.length);
        // folders have a trailing slash that's not truly part of the name
        if (s.slice(-1) == '/')
            s = s.slice(0,-1);
        // it is encoded
        s = (decodeURIComponent || unescape)(s);        
        return s;
    } // getItemName

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Try escaping or quoting the apostrophe.

    But I have to say that I think it poor practice (although not illegal) to include an apostrophe in a file name.

    All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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