3

When fetching the output of scandir() function of php, I have some folders where there names are in arabic language. The problem is that the output was "???????" in order to display Arabic letters.

Here is a piece of my code:

<?php
$dir    = '../dirs';
$files1 = scandir($dir);
foreach($files1 as $key => $value)
{
    if($value != '.' && $value != '..' && $value != '')
    echo "<option>".$value."</option>";
}
?>

Any ideas?

  • Are you running PHP 5.x? If so, then it's known that this won't work because `scandir` will not properly interpret unicode characters. Try PHP6. – David-SkyMesh Aug 18 '13 at 07:42
  • @David-SkyMesh yes, I am working with PHP 5.x (xamp server) –  Aug 18 '13 at 07:43
  • @Borko not a file, it's the filename being returned by `scandir()`. – David-SkyMesh Aug 18 '13 at 07:43
  • PHP5.x is not good with unicode. You might try using `$results = \`ls ../dirs\`` instead. – David-SkyMesh Aug 18 '13 at 07:44
  • What encoding file names are returned in depends on the OS and file system. Essentially PHP returns the raw bytes the OS is providing, and international file name handling can vary widely and be pretty messy among different systems. So, it's a messy situation you'll have to supply more details about if you need a solution. – deceze Aug 18 '13 at 08:44
  • @David Stop that FUD please. PHP is as bad or good as other systems with Unicode if you know what you're doing. And PHP 6 doesn't exist. – deceze Aug 18 '13 at 08:45
  • Possible duplicate of [List directories containing Unicode characters on Windows](https://stackoverflow.com/questions/10008341/list-directories-containing-unicode-characters-on-windows) – user.dz Apr 07 '18 at 23:09

3 Answers3

1

you should use <meta charset="utf-8"> in your html page.
also take a look at this alternative, this may solve the problem:

$dir = "/tmp";
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

print_r($files);


also run your script with command line, this may give you some clues.
I don't know if this one will work or not.but anyway here it is:

if(PHP_OS == "WIN")
 $out = system("dir");
else
 $out = system("ls");

echo $out;


see results to find out if the problem still exist

Soosh
  • 812
  • 1
  • 8
  • 24
1

In PHP5, I have yet to this day found a way to work with files that have extended characters in their filename. I am led to believe that this is a problem within PHP itself, which is why version 6 is taking so long to develop. (PHP6 is scheduled to have full Unicode support.)

There is absolutely no problems with reading and writing to files that contain extended characters in their content, but the filenames themselves cannot contain them. This is currently the only limitation that I personally haven’t found a workaround for. Fortunately, it’s usually not an issue since web-safe filenames don’t current include extended characters which cause problems.

Houssam Badri
  • 2,441
  • 3
  • 29
  • 60
0

You can try it !

 $files1 = scandir($dir);  
    foreach ($files1 as $file) {
       echo  utf8_encode($file);
       echo '<br>';
    }
ttmanh
  • 3
  • 3