Page 1 of 1
QUICK QUESTION
Posted: Fri Jun 05, 2009 3:42 pm
by sirstrumalot
Can I set something in JS or an other language in a form so that when someone clicks "Browse" to upload a file that the file extension is already set in the upload window for a particular type of file, like a .jpeg? This way they can't upload, for example, a csv because they can't see it.
Re: QUICK QUESTION
Posted: Fri Jun 05, 2009 4:17 pm
by mikemike
I think you'll probably need some sort of Java applet for this, never seen it done in JS before
Re: QUICK QUESTION
Posted: Fri Jun 05, 2009 5:42 pm
by requinix
Can't be done in JavaScript (or HTML or anything else of that nature).
Re: QUICK QUESTION
Posted: Fri Jun 05, 2009 7:41 pm
by mikemike
I don't see how it can be done in HTML? Or even JavaScript easily...
Re: QUICK QUESTION
Posted: Sun Jun 07, 2009 12:36 am
by sirstrumalot
I was talking about this:
Code: Select all
<input type="file" accept="image/jpeg">
But this doesn't seem to work with my forms. I'm assuming it's either because i'm writing in PHP or because i'm using XHTML. I'm not certain why it's not working, but the file types like "text/csv" or "image/jpeg" are standard MIME types for the file type of input. This way the extension shows in the browse box. But it's not working. Ideas?
Re: QUICK QUESTION
Posted: Sun Jun 07, 2009 3:33 am
by requinix
input's accept attribute is official - it's defined in all the HTML standards - but there aren't any browsers that implement it. That's why it isn't working.
MIME types, by the way, have nothing to do with the extension and more about the data contained in the file. But since you're really trying to get a type of file and not just any file with a specific extension, that wouldn't be a problem.
So a clarification to my earlier post:
You can specify the type(s) of file accepted but the <input>, but it doesn't work. The W3C guys even recommend against using it, stating (correctly) that file validation should be done on the server, not the client.
Re: QUICK QUESTION
Posted: Mon Jun 08, 2009 2:23 am
by sirstrumalot
That's what I was looking for. I appreciate it!
btw, would you recommend a method of server side validation? Currently i'm using javascript to validate the file extension onchange of the input, but that doesn't stop them, it only alerts them. What methods are out there to ensure it is what I want them to upload?
Thanks again!
Re: QUICK QUESTION
Posted: Mon Jun 08, 2009 2:53 am
by onion2k
Don't use the file extension. It means very little. Don't use the MIME type either. Both of them are content sumbitted by the user and therefore cannot be trusted.
If you want to be sure something is a JPEG image, use getimagesize(). One of the returned values is the image type.
Re: QUICK QUESTION
Posted: Mon Jun 08, 2009 3:59 am
by sirstrumalot
Very good recommendation! They can always change the extension. However, i'm curious how to do this if it's a csv.... thoughts?