检查上传的文件是以csv格式 [英] Check file uploaded is in csv format
问题描述
if(($ _FILES [file] [type] ==text / csv))
{
}
else
{
}
如果我改变了内容类型,那么它的作用就是不是csv。
mime类型可能不是 text / csv
有些系统可以读/保存它们不同。 (例如,有时IE发送.csv文件为 application / vnd.ms-excel
),所以你最好打赌是建立一个允许值的数组和测试,然后找到所有可能的值来测试。
$ b $ pre $ $ m $ c $ $ mimes = array('application / vnd.ms-excel','text / plain','text / csv' '文本/ TSV');
if(in_array($ _ FILES ['file'] ['type'],$ mimes)){
//做某事
} else {
die(Sorry, mime类型不允许);
$ b
如果你希望你可以添加进一步的检查,如果MIME返回为文本/很简单,你可以运行一个 preg_match
来确保它有足够的逗号是一个csv。
I am uploading a file in php and only want to upload it if it's a csv file. I believe my syntax is right for the content type. It always goes to else statement when it's a csv file. What I am doing wrong here?
if (($_FILES["file"]["type"] == "text/csv"))
{
}
else
{
}
If I change the content type it works for that format just not csv.
the mime type might not be text/csv
some systems can read/save them different. (for example sometimes IE sends .csv files as application/vnd.ms-excel
) so you best bet would be to build an array of allowed values and test against that, then find all possible values to test against.
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
if(in_array($_FILES['file']['type'],$mimes)){
// do something
} else {
die("Sorry, mime type not allowed");
}
if you wished you could add a further check if mime is returned as text/plain you could run a preg_match
to make sure it has enough commas in it to be a csv.
这篇关于检查上传的文件是以csv格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!