Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a file upload is a valid CSV file - or at least text - in ColdFusion 8?

I have a form which allows a user to upload a file to the server. How can I validate that the uploaded file is in fact the expected format (CSV, or at least validate that it is a text file) in ColdFusion 8?

like image 598
Eric Belair Avatar asked Oct 11 '22 21:10

Eric Belair


1 Answers

For simple formats like CSV, just check yourself, for example via regex.

 <cffile action="read" file="#uploadedFile#" variable="contents" charset="UTF-8">

 <cfset LooksLikeCSV = REFind("^([^;]*;)+[^;]*$", contents)>

You can place additional checks with regard to file size limits or forbidden characters.

For other file formats, you can check for header signatures that occur in the first few bytes of the file.

You could even write a full parser for your expected file format - for CSV validation, you could do a ListToArray() at CR/LF and check each line individually against a regex. XML should work pretty straightforward as well - just try to pass it to XmlParse(). Binary formats like images are a little more difficult, but libraries exist there as well.

like image 75
Tomalak Avatar answered Oct 14 '22 03:10

Tomalak