Solved

Javascript > Filter upload dialog

Posted on 2004-08-18
4
542 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now