|
|
|
|
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
10-10-2005, 10:10 AM
|
#1
|
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
Posts: 46
|
$_files
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
|
|
10-10-2005, 11:25 AM
|
#2
|
Status: Member
Join date: Feb 2005
Location: UK
Expertise:
Software:
Posts: 335
|
You can't really do much else other than judging by the extension.
The best you could probably do is remove all illegal characters from it and just serve it as text.
|
|
10-10-2005, 12:46 PM
|
#3
|
Status: Member
Join date: Jul 2005
Location: Massachusetts, US
Expertise:
Software:
Posts: 428
|
Yeah, checking the extension is the only way. Those weird characters (ASCII, Symbols?) can be put into a text file. So like no longer than 10000 lines?
|
|
10-10-2005, 01:16 PM
|
#4
|
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
Posts: 3,820
|
You could rely on the file's extension for some soft protection. Obviously that wouldn't stop people uploading an image (or any other type) file with a .txt extension. However, if you force the server (using .htaccess or some such device) to only output plain text from the folder(s) where the files are being stored then their uploading any other type of file than plain text would be pointless.
|
|
10-10-2005, 04:03 PM
|
#5
|
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
Posts: 46
|
thanx to all for your time,
I use :
if ($_FILES['userfile']['type']='text/plain')
{then OK}
else
{wrong file type}
but it doesn't seem to work. PHP uploads both text files and .exe files.
Any thoughts?
|
|
10-10-2005, 06:04 PM
|
#6
|
Status: I love this place
Join date: Jan 2005
Location: UK, Birmingham
Expertise:
Software:
Posts: 606
|
you should be using == for comparision, = is used for assigning. You cannot assign in if functions
|
|
10-12-2005, 10:20 PM
|
#7
|
Status: I'm new around here
Join date: Aug 2005
Location: Flint, MI
Expertise:
Software:
Posts: 7
|
You could try this
PHP Code:
$file = substr($_FILES['userfile']['name'], -3);
if($file != "txt") {
echo "Invaild file type"; exit;
}
else {
//Do whatever is needed to be done
}
|
|
10-18-2005, 12:28 PM
|
#8
|
Status: Junior Member
Join date: Oct 2005
Location:
Expertise:
Software:
Posts: 46
|
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
Thread Tools |
|
Display Modes |
Hybrid Mode
|
|
|