Validating file upload type (CSV)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cuzvinn
Forum Newbie
Posts: 1
Joined: Mon Jan 19, 2004 1:29 pm
Contact:

Validating file upload type (CSV)

Post by cuzvinn »

I am working on code that allows a user to create a CSV (comma delimited) file in excel and upload it. However, I am running into problems with file type. On upload, I check that the MIME type of the file is text/plain. This works great when the file is uploaded from a computer that does not have Microsoft Excel. However, when the CSV file is uploaded from a computer with Excel, the MIME type is sent as application/octet stream. I am looking for suggestions on other secure ways to validate file types. Any help is appreciated.
Thanks
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post by DuFF »

I would recommend mime types, but since that doesn't seem to be working the best way would probably be to get the last 3 characters in the filename and check them. An example:

Code: Select all

<?php
$filename = "example.txt";
$extension = substr($filename, -3);  //returns "txt"
if ($extension == "txt")
{
//do upload
}
else
{
        echo "Only .txt files are allowed";
}
?>
Post Reply