【问题标题】:Cannot upload .csv file (MIME type issue?) [duplicate]无法上传 .csv 文件(MIME 类型问题?)[重复]
【发布时间】:2016-03-03 09:03:12
【问题描述】:

我想在我的服务器上上传三个文件(.txt、.csv 和 .xml)。 .txt 和 .xml 与以下代码完美配合。只是,我无法上传 .csv 文件(没有任何浏览器!)。我得到的错误是:错误!不可用的文件类型:application/x-csv

有办法解决吗?

代码:

if (isset($_POST['Form']) && isset($_FILES['file'])) {
    $types = array('xml', 'csv', 'txt');
    $ftypes = array('text/xml', 'application/vnd.ms-excel', 'text/plain');
    if (!in_array($_POST['Form']['type'], $types)) {
    $errors[] = 'Undefined type';
    } elseif (intval($_FILES['file']['size']) == 0) {
    $errors[] = 'Zero file size';
    } elseif (!in_array($_FILES['file']['type'], $ftypes)) {
    $errors[] = 'Unavailable file type: ' . $_FILES['file']['type'];
    } else {
    $parent_id = intval($_POST['Form']['parent']);
    switch ($_POST['Form']['type']) {
        case 'xml':
        if ($_FILES['file']['type'] != 'text/xml')
            $errors[] = "Choosen type and file mime-type doesn't match";
        else {
            require_once(__DIR__ . '/parsers/xml.php');
        }
        break;
        case 'csv':
        if ($_FILES['file']['type'] != 'application/vnd.ms-excel')
            $errors[] = "Choosen type and file mime-type doesn't match";
        else {
            require_once(__DIR__ . '/parsers/csv.php');
        }
        break;
        case 'txt':
        if ($_FILES['file']['type'] != 'text/plain')
            $errors[] = "Choosen type and file mime-type doesn't match";
        else {
            require_once(__DIR__ . '/parsers/txt.php');
        }
        break;
    }
    }
}
?>

【问题讨论】:

    标签: php csv


    【解决方案1】:

    还有一种检查文件扩展名的简单方法,如下所示:

    // first get the file extension
    $ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));  // csv
    
    $types = array('xml', 'csv', 'txt');
    
    // now check against permissible extensions
    if(in_array($ext, $types)){
        // allowed
    }else{
        // not allowed
    }
    

    已编辑:

    if (isset($_POST['Form']) && isset($_FILES['file'])) {
    
        $ext = strtolower(pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION));  // csv
        $types = array('xml', 'csv', 'txt');
    
        if (!in_array($ext, $types)) {
            $errors[] = 'Invalid file type';
        }elseif(intval($_FILES['file']['size']) == 0) {
            $errors[] = 'Zero file size';
        }else{
            // $parent_id = intval($_POST['Form']['parent']); you can uncomment it if necessary
            switch ($ext) {
                case 'xml':
                    require_once(__DIR__ . '/parsers/xml.php');
                    break;
                case 'csv':
                    require_once(__DIR__ . '/parsers/csv.php');
                break;
                case 'txt':
                    require_once(__DIR__ . '/parsers/txt.php');
                break;
            }
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      根据RFC4180,csv脚本的mime类型应该是text/csv

      【讨论】:

        猜你喜欢
        • 2015-01-11
        • 1970-01-01
        • 2013-05-24
        • 2022-12-15
        • 1970-01-01
        • 2021-11-15
        • 2010-10-16
        • 2013-10-20
        相关资源
        最近更新 更多