Limiting uploads to a file type

  • tastysite
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 09, 2008
  • Posts: 349
  • Loc: Brighouse, West Yorkshire, England
  • Status: Offline

Post April 11th, 2008, 9:43 am

How would you go about limiting uploads using the Browse... button. Like they do on newgrounds.com when you upload a flash game or movie if you try to upload a file that is not a .fla for instance a .doc or whatever it work work? How is this achieved? The browse button I'm using runs on html and all it is is this
Code: [ Select ]
<input name="game" type="file" id="game" size="50">
please help :!: :!: :!:
^__^
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post April 11th, 2008, 9:43 am

  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post April 11th, 2008, 7:58 pm

I would think newgrounds is using Flash for their file uploads, Flash gives you all kind of usefull information about a file before upload. I don't know though.


For a normal HTML file field, I believe you can check the value of the file input and grab the extension of the filename that's returned.

Code: [ Select ]
<script type="text/javascript">
function check_file(){
var allowed = ['unr', 'utx'];
var game = document.getElementById('game').value;
var is_allowed = new RegExp(allowed.join('$|') + '$', 'i');
if(is_allowed.test(game))
{
   return true;
}
alert('this type of file is not allowed');
return false;
</script>
<form onsubmit="return check_file();" action="">
<!-- ... -->
  1. <script type="text/javascript">
  2. function check_file(){
  3. var allowed = ['unr', 'utx'];
  4. var game = document.getElementById('game').value;
  5. var is_allowed = new RegExp(allowed.join('$|') + '$', 'i');
  6. if(is_allowed.test(game))
  7. {
  8.    return true;
  9. }
  10. alert('this type of file is not allowed');
  11. return false;
  12. </script>
  13. <form onsubmit="return check_file();" action="">
  14. <!-- ... -->
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post April 12th, 2008, 4:29 pm

You can also user PHP for this... I think :lol:
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • joebert
  • Sledgehammer
  • Genius
  • No Avatar
  • Joined: Feb 10, 2004
  • Posts: 13455
  • Loc: Florida
  • Status: Offline

Post April 12th, 2008, 7:41 pm

Well, you're going to need to use a server-side language of some sort to process the upload, yes.

But wouldn't you rather save everyones' time by checking the filetype before actually uploading the file ?
I'm not talking about the ones the PHP security is designed to protect against, I'm refering to actual people wo use the form as intended. ;)
Strong with this one, the sudo is.
  • Bogey
  • Bogey
  • Genius
  • User avatar
  • Joined: Jul 14, 2005
  • Posts: 8211
  • Loc: USA
  • Status: Offline

Post April 12th, 2008, 7:47 pm

By "You can also use PHP for this... I think" I meant that you can user PHP to check file type... not the whole entire process.
"Bring forth therefore fruits meet for repentance:" Matthew 3:8
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Joined: May 22, 2004
  • Posts: 3415
  • Loc: Richland, WA
  • Status: Offline

Post April 12th, 2008, 11:36 pm

I'm confused by that last statement :scratchhead:

I went and had a look at what newgrounds does and they use a javascript function to check if the file is the correct extension such as what Joebert has presented. However, theirs is on change for the file input. So when you select a file it tells you that its wrong.

if you want something like that you can use the javascript function above and use this line for your file.

Code: [ Select ]
<input name="game" type="file" id="game" size="50" onchange="return check_file();">
#define NULL (::rand() % 2)

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 134 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.