Limiting uploads to a file type

  • tastysite
  • Proficient
  • Proficient
  • User avatar
  • Posts: 349
  • Loc: Brighouse, West Yorkshire, England

Post 3+ Months Ago

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
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

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. <!-- ... -->
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

You can also user PHP for this... I think :lol:
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

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. ;)
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8388
  • Loc: USA

Post 3+ Months Ago

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.
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

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();">

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 31 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
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.