Forum Moderators: coopster

Message Too Old, No Replies

$_files

upload only TEXT files

         

ktsirig

10:00 am on Oct 10, 2005 (gmt 0)

10+ Year Member



Hi all!

I have a question: I am constructing a form where the user can upload a file.
I need however to verify that the file uploaded is a TEXT file.

Is $_FILE['userfile']['type'] what I need? I am asking this because no matter if I upload a .txt or a .gif file, PHP says they are both $_FILE['userfile']['type']='text' and so, they are both uploaded on the server, whereas there should be only .txt file uploaded and not the .gif file.

In general, how can I allow only text files to be uploaded?
(Of course, i can't rely much on the extension I believe)..

Thanx

dreamcatcher

12:04 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi ktsirig,

You can check the extension of the file using substr.

$name = $_FILES['userfile']['name'];

if (substr(strtolower($name),0,-3)=='txt'))
{
//file ok
}
else
{
//not ok
}

dc

henry0

1:52 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dreamcatcher,
Don't you have one too many parentheses at the end of "if"?

I could be wrong :)

dreamcatcher

2:10 pm on Oct 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hee hee, yep, you`re right henry0. Cheers.

Should be:

$name = $_FILES['userfile']['name'];

if (substr(strtolower($name),0,-3)=='txt')
{
//file ok
}
else
{
//not ok
}

dc