Excel doesn't do a very good job on exporting comma separated files (CSV or TXT). Reading the file with fgetcsv could fail, since the field separator might not be as expected (a comma).

Here is a quick method to find the field delimiter from a CSV file. We'll try to search usual delimiters and return the delimiter who finds the most rows in the first line.

/**
 * Detect field separator from CSV file
 * @param string $csvFile Path to CSV file
 * @return string Delimiter
 */
public static function detectCSVFileDelimiter($csvFile)
{
    $delimiters = array(
        ',' => 0,
        ';' => 0,
        "\t" => 0,
        '|' => 0,
    );
    $firstLine = '';
 
    $handle = fopen($csvFile, 'r');
    if ($handle) {
        $firstLine = fgets($handle);
        fclose($handle);
    }
 
    if ($firstLine) {
        foreach ($delimiters as $delimiter => &$count) {
            $count = count(str_getcsv($firstLine, $delimiter));
        }
        return array_search(max($delimiters), $delimiters);
    } else {
        return key($delimiters);
    }
}

Neat, ha ?