public function CsvMimeDetector::detect in MimeDetect 8
Try MIME detection on a given file.
Parameters
string $path: Path of the file to be analyzed.
Return value
string The detected MIME, NULL if file contents are not recognized.
Overrides MimeDetectorInterface::detect
File
- src/
Plugin/ MimeDetector/ CsvMimeDetector.php, line 21
Class
- CsvMimeDetector
- Provides a CSV MIME detector.
Namespace
Drupal\mimedetect\Plugin\MimeDetectorCode
public function detect($path) {
if (($handler = @fopen($path, 'r')) !== FALSE) {
// Check first line, it must be printable text.
$first_line = fgets($handler, 16384);
if ($first_line && strlen($first_line) && ctype_print(str_replace([
"\n",
"\r",
], '', $first_line))) {
rewind($handler);
// Read the first two lines, they must have the same number of columns.
$columns = [];
while (($data = fgetcsv($handler)) !== FALSE && count($columns) < 2) {
$columns[] = count($data);
}
if (count($columns) == 1 || $columns[0] == $columns[1]) {
return 'text/csv';
}
}
}
return NULL;
}