检查上传的文件是以csv格式 [英] Check file uploaded is in csv format

查看:396
本文介绍了检查上传的文件是以csv格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上传一个文件在PHP中,只想上传它,如果它是一个CSV文件。我相信我的语法对于内容类型是正确的。当它是一个csv文件时,它总是会去else语句。我在这里做错了什么?

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆