Solved

Javascript > Filter upload dialog

Posted on 2004-08-18
4
546 Views
Last Modified: 2006-11-17
How can i filter an upload dialog (using Browse control) to only dispay images? (GIF, PNG, JPG, JPEG, BMP)

Thanks
0
Comment
Question by:shaun341
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 3

Expert Comment

by:Squew01
ID: 11856108
W3C (web standard) defines the 'accept' argument to use like this:
<input type="file" accept="image/gif">
but unfortunatly this seems to work only in some version of netscape!!!!!
Sorry...

A good solution would be to use a server side script based on mime type of the uploaded file.
This is quite easy to do with PHP. Ask me for some help if you wanna...

/squew/
0
 
LVL 1

Author Comment

by:shaun341
ID: 11937431
Thanks for your reply, its not really an option in javascript it appears then.

How can i do it in PHP?
0
 
LVL 3

Accepted Solution

by:
Squew01 earned 50 total points
ID: 11940523
You can verify the kind of file using the mime-type. This info is givent by the browser to the web server.

When you upload a file with PHP, the browser gives infos about the uploaded file. Those info are automaticly stored into variables.
Ex. if you store the uploaded file in the $file var, $file_type will store the mime type of the file. (more info here: http://www.zend.com/manual/features.file-upload.php)

So, you just have to check the mime-type of the file, like this:

if(($file_type!="image/x-png")&&($file_type!="image/pjpeg")&&($file_type!="image/gif")&&($file_type!="image/bmp")&&($file_type!="text/xml"))
{
//file is not an image
}
else
{
//file is an image
}

Here is a list of existing mime-type :

image/gif
image/jpeg
image/pjpeg
image/tiff
image/x-png
image/x-xbitmap
image/bmp
image/x-jg
image/x-emf
image/x-wmf

Hopes this help and sorry for my english which is not as good as I want.

/squew/
0

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question