PHP fgetcsv() delimiter ';' not recognized

The second parameter is the length, so your fgetcsv should be

fgetcsv($handle, 0, ';');

Resulting in

4 champs à la ligne 1: 

First Name
Last Name
Email
Age
4 champs à la ligne 2: 

Julie
Brown
[email protected]
52
4 champs à la ligne 3: 

Dan
Wong
[email protected]
19
4 champs à la ligne 4: 

Tim
Hortons
[email protected]
27

As for your second question on variable delimiters. By far the easiest method would allow the user to define which delimiter to use on the upload form, possibly using a select element of acceptable delimiters and then use it when reading the csv.

For Example

$allowedDelimiters = [',', ';'];
$defaultDelimiter = ';';
if (true === empty($_POST['delimiter'])) {
    $_POST['delimiter'] = $defaultDelimiter;
}
if (!in_array($_POST['delimiter'], $allowedDelimiters, true)) {
    $_POST['delimiter'] = $defaultDelimiter;
    //alternatively redirect back to the form with an error message
}
$delimiter = $_POST['delimiter'];

You can also parse the lines checking for the desired delimiter.

$filename = "upload/".$_FILES['fichier']['name'];
if (($handle = fopen($filename, "r")) !== false) {
    $content = fread($handle, filesize($filename));
    fclose($handle);
    //count the delimiters
    $semiColons = substr_count($content, ';');
    $commas = substr_count($content, ',');
    //read each row
    $rows = str_getcsv($content, "\n");
    $rowCount = count($rows);
    $delimiter = null;
    foreach ($rows as $line => $row) {
        //check the delimiters
        if (false === isset($delimiter)) {
            /* 
              determine if the delimiter total divided by the number 
              of rows matches the delimiters found on this row 
              and use it to parse the columns 
            */
            if ($semiColons > 0 && $semiColons / $rowCount === substr_count($row, ';')) {
                $delimiter = ';';
            } elseif ($commas > 0 && $commas / $rowCount === substr_count($row, ',')) {
                $delimiter = ',';
            }
        }
        //read the columns using the detected delimiter 
        //otherwise use a default if a delimiter could not be determined
        $columns = str_getcsv($row, $delimiter ? : ';');
        echo "<p>$rowCount champs à la ligne " . ($line + 1) . "</p>\n";
        foreach ($columns as $column) {
            echo $column . "<br/>\n";
        }
    }
}

array fgetcsv ( resource $handle [, int $length = 0 [, string $delimiter = "," [, string $enclosure = '"' [, string $escape = "\" ]]]] )

Manual

Second parameter is the length.

fgetcsv($handle, 0, ";")

Tags:

Php

Csv