php 检查上传的文件是否为 csv 格式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6654351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:55:04  来源:igfitidea点击:

Check file uploaded is in csv format

phpsyntaxfile-uploadcontent-type

提问by theking963

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?

我正在上传一个 php 文件,如果它是一个 csv 文件,我只想上传它。我相信我的语法适合内容类型。当它是一个 csv 文件时,它总是转到 else 语句。我在这里做错了什么?

if (($_FILES["file"]["type"] == "text/csv"))
{

}
else
{

}

If I change the content type it works for that format just not csv.

如果我更改内容类型,它只适用于该格式而不适用于 csv。

回答by Alan Cole

the mime type might not be text/csvsome 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.

mime 类型可能不是text/csv某些系统可以读取/保存它们的不同类型。(例如,有时 IE 将 .csv 文件发送为application/vnd.ms-excel)所以你最好的办法是构建一个允许值的数组并对其进行测试,然后找到所有可能的值进行测试。

$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_matchto make sure it has enough commas in it to be a csv.

如果您希望可以进一步检查 mime 是否作为文本/纯文本返回,您可以运行 apreg_match以确保其中有足够的逗号作为 csv。

回答by ukliviu

There are a lot of possible MIME types for CSV files, depending on the user's OS and browser version.

CSV 文件有多种可能的 MIME 类型,具体取决于用户的操作系统和浏览器版本。

This is how I currently validate the MIME types of my CSV files:

这是我目前验证 CSV 文件的 MIME 类型的方式:

$csv_mimetypes = array(
    'text/csv',
    'text/plain',
    'application/csv',
    'text/comma-separated-values',
    'application/excel',
    'application/vnd.ms-excel',
    'application/vnd.msexcel',
    'text/anytext',
    'application/octet-stream',
    'application/txt',
);

if (in_array($_FILES['upload']['type'], $csv_mimetypes)) {
    // possible CSV file
    // could also check for file content at this point
}

回答by Eddie

You can't always rely on MIME type..

你不能总是依赖 MIME 类型..

According to: http://filext.com/file-extension/CSV

根据:http: //fileext.com/file-extension/CSV

text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext

There are various MIME types for CSV.

CSV 有多种 MIME 类型。

Your probably best of checking extension, again not very reliable, but for your application it may be fine.

您可能最好的检查扩展,同样不是很可靠,但对于您的应用程序可能没问题。

$info = pathinfo($_FILES['uploadedfile']['tmp_name']);

if($info['extension'] == 'csv'){
 // Good to go
}

Code untested.

代码未经测试。

回答by Bhavesh Garg

As you are worried about user upload other file by mistake, I would suggest you to use accept=".csv"in <input>tag. It will show only csv files in browser when the user uploads the file. If you have found some better solution then please let me know as I am also trying to do same and in the same condition - 'trusted users but trying to avoid mistake'

由于您担心用户错误地上传其他文件,我建议您使用accept=".csv"in<input>标签。当用户上传文件时,它只会在浏览器中显示 csv 文件。如果您找到了更好的解决方案,请告诉我,因为我也在尝试在相同的条件下做同样的事情 - “受信任的用户,但要避免错误”

回答by Garvin

So I ran into this today.

所以我今天遇到了这个。

Was attempting to validate an uploaded CSV file's MIME type by looking at $_FILES['upload_file']['type'], but for certain users on various browsers (and not necessarily the same browsers between said users; for instance it worked fine for me in FF but for another user it didn't work on FF) the $_FILES['upload_file']['type']was coming up as "application/vnd.ms-excel" instead of the expected "text/csv" or "text/plain".

试图通过查看来验证上传的 CSV 文件的 MIME 类型$_FILES['upload_file']['type'],但对于各种浏览器上的某些用户(并且所述用户之间不一定使用相同的浏览器;例如,它在 FF 中对我来说工作正常,但对于另一个用户它不起作用在 FF 上)出现的$_FILES['upload_file']['type']是“application/vnd.ms-excel”,而不是预期的“text/csv”或“text/plain”。

So I resorted to using the (IMHO) much more reliable finfo_* functions something like this:

所以我求助于使用(恕我直言)更可靠的 finfo_* 函数是这样的:

$acceptable_mime_types = array('text/plain', 'text/csv', 'text/comma-separated-values');

if (!empty($_FILES) && array_key_exists('upload_file', $_FILES) && $_FILES['upload_file']['error'] == UPLOAD_ERR_OK) {
    $tmpf = $_FILES['upload_file']['tmp_name'];

    // Make sure $tmpf is kosher, then:

    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime_type = finfo_file($finfo, $tmpf);

    if (!in_array($mime_type, $acceptable_mime_types)) {
        // Unacceptable mime type.
    }
}

回答by Vaibhav Shahu

Mime type option is not best option for validating CSV file. I used this code this worked well in all browser

Mime 类型选项不是验证 CSV 文件的最佳选择。我使用了这段代码,它在所有浏览器中都运行良好

$type = explode(".",$_FILES['file']['name']);
if(strtolower(end($type)) == 'csv'){

}
else
{

}

回答by manish yadav

simple use "accept" and "required" in and avoiding so much typical and unwanted coding.

简单地使用“accept”和“required”,避免太多典型和不需要的编码。