Check whether an uploaded file is a CSV file
I want to test if the uploaded file is a CSV file, after selecting a csv file then confirm with this code and still give me the message that开发者_StackOverflow中文版 not a CSV file please help me out.
if($_FILES["file"]["type"] <> "application/vnd.ms-excel"){
die("This is not a CSV file.");
}
The simplest check is "does the file name end in .csv
". This will get you most of the way, but it is possible that someone could try and upload something else with a CSV extension.
Your next check should be that the file is text (rather than binary; note that Excel files should fail this). To do this, check the ascii representation of each character: apart from carriage returns, line feeds and tabs, there should be no values less than 32.
Once you know that the file is text, you can run it through a CSV parser and see if the result is nonsense or not.
精彩评论