upload form limiting

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
SL-Cowsrule
Forum Newbie
Posts: 13
Joined: Sat Oct 12, 2002 5:08 pm

upload form limiting

Post by SL-Cowsrule »

Im not sure if i can use MIME types for this, but if you know how to limit the kind/type of file that can be uploded, please post here.

Thanks,
CoW
daynah
Forum Newbie
Posts: 6
Joined: Fri Oct 25, 2002 12:48 pm
Location: USA

Post by daynah »

I think the best thing to do is to check the file type after it's been uploaded temporily.

You can check the array $HTTP_POST_FILES['userfile']['tmp_name'] if you are using php.

Something like

Code: Select all

<?php
if ($HTTP_POST_FILESї'imagefile']ї'type'] == "image/gif")
{ 
     // do something here
}
else
{
   echo 'sorry, wrong file type!';
}
?>
You can read an article about it here:
http://www.phpfreaks.com/tutorials/36/0.php
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

http://www.w3.org/TR/html4/interact/for ... def-accept
i.e.

Code: Select all

<input type="file" accept="text/*" name="ufile" />
will accept all text-files (text/plain, text/html, ....).
But that's courtesy of the browser, so a server-side check is mandatory to be sure
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

Or you could check the extension

Code: Select all

&lt;?php
if(substr($file,-1,-3) == "gif") {
 echo "It's a GIF file";
}
?&gt;
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Or you could check the extension
that's exactly what I hate windows for ;)
And sometimes file-extensions are longer than 3 chars (i.e. index.html)
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

but you'll know that cos you're checking it (so it'll become "tml").
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

only as example:
what ever 'Procedure Builder' is ;)
it uses .tml as extension for "Export for Task Manager file"
http://www.aiai.ed.ac.uk/~entprise/ente ... BUIL94.htm :D
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

ok how about changing the code then?

substr($file,-1,-4);

Then for 3 character exntesion add "." at the start and it'll be alright for 4 character ones. :D
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

naaa, I'll wait for mime-magic.
Until than I use

Code: Select all

substr($fname, strrpos($fname, '.'))
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

that's clever script that is... Never thought of it. Thanks I can use that for my script :D
User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

why not just use strrchr()

Code: Select all

$str = "some.weird.file.name";
$result = strrchr($str, ".");
//$result now equals ".name"
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

because I come from the C++-world and some php-return-values are uncommon to me ;)
Post Reply