Ad

Laravel's File::files Function Does Not Return File Names Containing Chinese Characters

- 1 answer

currently I have something working like the following and it works with english file names.

$filePaths = File::files(storage_path() . '/files/somepath');
foreach($filePaths as $path){
    array_push($somearray, pathinfo($path, PATHINFO_BASENAME));
}

$filePaths does not contain chinese file names.

How can I solve this problem? Is there another way to work around?

Ad

Answer

This issue isn't related to Laravel, but to PHP pathinfo() function which can't handle non-standard chars like chinese.

Workaroud is to make your own function and use it instead of pathinfo():

if (!function_exists("mb_pathinfo"))
{
  function mb_pathinfo($path, $opt = "")
  {
    $separator = " qq ";
    $path = preg_replace("/[^ ]/u", $separator."\$0".$separator, $path);
    if ($opt == "") $pathinfo = pathinfo($path);
    else $pathinfo = pathinfo($path, $opt);

    if (is_array($pathinfo))
  th, $opt);

    if (is_array($pathinfo))
    {
      $pathinfo2 = $pathinfo;
      foreach($pathinfo2 as $key => $val)
      {
        $pathinfo[$key] = str_replace($separator, "", $val);
      }
    }
    else if (is_string($pathinfo)) $pathinfo = str_replace($separator, "", $pathinfo);
    return $pathinfo;
  }
}

Related question: Make PHP pathinfo() return the correct filename if the filename is UTF-8

Ad
source: stackoverflow.com
Ad