Wednesday 10 August 2016

Check CSV File is in Valid Format in PHP

Function to check whether a file is a valid CSV file. This function basically checks file for the allowed csv meme types rather checking for the content of the csv file for the format.

function isCSV($filename) {
    $allowed_mime_types = [
        'text/csv',
        'text/plain',
        'text/comma-separated-values',
        'application/vnd.ms-excel',
        'application/csv',
        'application/vnd.msexcel',
        'text/anytext',
        'application/octet-stream',
        'application/txt',
        'application/excel',
    ];
    $file_info = file_info_open(FILEINFO_MIME_TYPE);
    $file_mime_type = file_info_file($file_info, $filename);

    return in_array($file_mime_type, $allowed_mime_types);
}


No comments:

Post a Comment