Checking for a .csv file

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
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Checking for a .csv file

Post by social_experiment »

I'm creating a script that will allow csv files to be uploaded; my problem is how to determine that the uploaded file is actually a .csv file. Found the url below and it has a few interesting points;
http://stackoverflow.com/questions/6654 ... csv-format

The option that stands out for me at this point is to validate the data in the file by using regular expressions - but i'm not sure if this is a workable option. The uploaded files will contain at least 1000 "records" at a time so using regex will probably slow things down significantly

Any ideas or suggestions on how to validate a csv file before uploading it?
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Checking for a .csv file

Post by Celauran »

I think they've probably nailed it; there's no real way short of parsing the file to be sure. I'd check MIME type against an array of possible values first, then try to parse the first line or two of the file. If they look OK, I'd assume the file was good.
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Checking for a .csv file

Post by social_experiment »

Currently i loop through the file and if an array is present i process it; i agree with the combination option, i think i will implement it with my existing code.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
Post Reply