I have a native PHP function issue

When use Php readdir native function, names of the files that used Cyrillic font appear to be different. By different, I mean “the names look like question marks like this ????” Please solve this issue. Please refer to below for an example.

$dir = "C:/xampp/hdocs/Terminal/uploads20180427te";
function ReadFolderDirectory($dir)
{
      $listDir = array();
      if($handler = opendir($dir)) {
            while (($sub = readdir($handler)) !== FALSE) {
                  if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
                        if(is_file($dir."/".$sub)) {
                              $listDir[] = $sub;
                        }elseif(is_dir($dir."/".$sub)){
                              $listDir[$sub] = ReadFolderDirectory($dir."/".$sub);
                        }
                  }
            }
            closedir($handler);
      }
      return $listDir;
}
$files = ReadFolderDirectory($dir);
var_dump($files );die;
bavuugarid DorjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
What do you get when you do this
$dir = "C:/xampp/hdocs/Terminal/uploads20180427te";
function ReadFolderDirectory($dir)
{
      $listDir = array();
      if($handler = opendir($dir)) {
            while (($sub = readdir($handler)) !== FALSE) {
                  if ($sub != "." && $sub != ".." && $sub != "Thumb.db") {
                        if(is_file($dir."/".$sub)) {
                              $listDir[] = $sub;
                        }elseif(is_dir($dir."/".$sub)){
                              $listDir[$sub] = ReadFolderDirectory($dir."/".$sub);
                        }
                  }
            }
            closedir($handler);
      }
      return $listDir;
}
$files = ReadFolderDirectory($dir);
header('Content-Type: text/html; charset=utf-8');
var_dump($files );die; 

Open in new window

The problem is not in the reading of the folder - the names there are correct - it is in how the browser is interpreting those characters. By setting the charset you are telling the browser to interpret the data as Unicode - so it should now display the names correctly.
bavuugarid DorjAuthor Commented:
Thank you for your help.

But after trying your code, I still have the same problem.
Julian HansenCommented:
What is your intention with the script

a) To display the names on the page
OR
b) To process the list for some other purpose?
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

bavuugarid DorjAuthor Commented:
My intention is to collect the names of files then insert it to the database.
Julian HansenCommented:
Then I would do a test run of the database insertion and see what the result is.

No point in trying to solve display errors when you are not going to display the results.

Try and do a test insert of the files into a test table and see what you get. Make sure the database is setup to support your character set though.
bavuugarid DorjAuthor Commented:
Thank you for your advice.

I'm looking forward to hear your test result.
Julian HansenCommented:
I'm looking forward to hear your test result.
Not following you - my recommendation was for you to do a test. I don't have filenames with Cyrillic characters to test with.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.