QUICK QUESTION

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sirstrumalot
Forum Commoner
Posts: 27
Joined: Mon May 18, 2009 10:26 pm

QUICK QUESTION

Post 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.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: QUICK QUESTION

Post by mikemike »

I think you'll probably need some sort of Java applet for this, never seen it done in JS before
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: QUICK QUESTION

Post by requinix »

Can't be done in JavaScript (or HTML or anything else of that nature).
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: QUICK QUESTION

Post by mikemike »

I don't see how it can be done in HTML? Or even JavaScript easily...
sirstrumalot
Forum Commoner
Posts: 27
Joined: Mon May 18, 2009 10:26 pm

Re: QUICK QUESTION

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: QUICK QUESTION

Post 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.
sirstrumalot
Forum Commoner
Posts: 27
Joined: Mon May 18, 2009 10:26 pm

Re: QUICK QUESTION

Post 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!
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: QUICK QUESTION

Post 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.
sirstrumalot
Forum Commoner
Posts: 27
Joined: Mon May 18, 2009 10:26 pm

Re: QUICK QUESTION

Post 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?
Post Reply