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
    Oct 2011
    Posts
    24
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Only offer specific uploadable file extensions in browse window

    Hi guys,

    I'm offering my users the option to upload jpg/png files and it works as it's supposed to. Some people are just to lazy to read though and still try to upload bmp/gif etc... My script prevents the files from being uploaded, but what I would love above all is only showing jpg/png files in the browse window. Is there any way to do this with PHP or do I have to look into another language?

    Thanks!

  • #2
    Senior Coder Rowsdower!'s Avatar
    Join Date
    Oct 2008
    Location
    Some say it's everything.
    Posts
    2,027
    Thanks
    5
    Thanked 397 Times in 390 Posts
    This is really a client-side thing (PHP can't help).

    There is an attribute called "accept" that can be used on an input element for file selection that would (theoretically) do this by specified MIME type, but it isn't really supported by browsers. The next best thing is to implement something in flash for file uploads. You would then have control to filter the filetypes the user sees by default in their file selection window.

    You can give SWFUpload a shot for starters (it seems to be a popular choice). It has a flag called "file_types" that can be set (as well as a description caption for the file types you choose to allow):

    Code:
        <script type="text/javascript">
            var swfu;
            window.onload = function () {
                swfu = new SWFUpload({// File Upload Settings
                    file_size_limit : "8 MB",   // 2MB
                    file_types : "*.jpg; *.gif",
                    file_types_description : "Image Files (JPG, GIF)",
                    file_upload_limit : "0",
                });
            };
        </script>
    The object of opening the mind, as of opening the mouth, is to shut it again on something solid. G.K. Chesterton
    See Mediocrity in its Infancy
    It's usually a good idea to start out with this at the VERY TOP of your CSS: * {border:0;margin:0;padding:0;}
    Seek and you shall find... basically:
    validate your markup | view your page cross-browser/cross-platform | free web tutorials | free hosting


  •  

    Posting Permissions

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