SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ban direct access to an input file ....

    Hi.
    I've to ban direct access to an input file
    I've managed a code like this:

    PHP Code:
    function ban(){
            
    alert('You must use only the browse buttom !');
            
    this.blur();
        }
    var 
    inputFile= $('img-upload');
     
    inputFile.onclick=ban
    I don't like very much.
    Do you have any suggestions ?


    Bye.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can still tab into the above example and type something

    Code:
    inputFile.onkeypress = function() {
        this.blur();
    }

  3. #3
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow what a quick reply
    Thanks a lot.

    I tried your code but I need that
    if you try to put text inside the
    input file it must be an empty field yet
    ( because of I check the field for an empty string.
    After submit the form I want to display
    a simple loader than server side I make
    a redirect to get rid of the loader so ....)

    so I've managed this code (quite the same):

    PHP Code:
    inputFile.onkeydown = function() {
                    
    this.value'';
                    
    this.blur();
                } 
    but I'm wondering are there security error troubles ?
    Last edited by whisher; Jan 10, 2008 at 09:40.

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    You can use the readonly attribute of the input element.

    Code HTML4Strict:
    <input type="text" id="inputFile" name="inputFile" readonly="readonly">

    Then you won't have to use Javascript at all and your web page is a lot safer. Such is the zen of Javascript.

  5. #5
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    You can use the readonly attribute of the input element.

    Code HTML4Strict:
    <input type="text" id="inputFile" name="inputFile" readonly="readonly">

    Then you won't have to use Javascript at all and your web page is a lot safer. Such is the zen of Javascript.
    Thanks for the point but in a input file type
    doesn't work

    PHP Code:
    <input type="file" name="img_upload" title="image upload" tabindex="2" id="img-upload"  readonly="readonly"/> 

    Bye.

  6. #6
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pmw57 View Post
    You can use the readonly attribute of the input element.
    This will not make it possible for the user to change the value, meaning that nothing will happen when pressing the browse button...

  7. #7
    SitePoint Guru whisher's Avatar
    Join Date
    May 2006
    Location
    Kakiland
    Posts
    732
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've changed my mind
    I was wrong because of I didn't take into account
    that in the script (server side) you can upload only
    images (gif,png,jpg) so I've managed this snippet

    PHP Code:
    var string'C:\Documents and Settings\whisher\Desktop\img001.jxpg';
    var 
    extstring.substr(string.lastIndexOf('.')+1,string.length).toLowerCase();
    alert(/^jpg$|^gif$|^png$/g.test(ext)); 
    What do you think about it ?


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
  •