I have a native PHP function issue

bavuugarid Dorj
bavuugarid Dorj used Ask the Experts™
on
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;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
Thank you for your help.

But after trying your code, I still have the same problem.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
My intention is to collect the names of files then insert it to the database.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Author

Commented:
Thank you for your advice.

I'm looking forward to hear your test result.
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial