try this code!
<?php
$handle = fopen("./test.txt", "r");
$serachEncoding = 'CP1251';
while (!feof($handle)) {
$buffer = fgets($handle, 4096);
$buffer = iconv("UTF8", "CP1251//TRANSLIT//IGNORE", $buffer);
$charset = mb_detect_encoding($buffer, [$serachEncoding]);
echo $charset;
}
fclose($handle);
if $serachEncoding = UTF8 the file will be read line by line and will output its encoding on each line
if $serachEncoding = CP1251 will output one line and its encoding
So why?