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
Solved

Javascript > Filter upload dialog

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

792 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