basename() fail when file name start by an accent

So as it turns out this seems to be a bug: https://bugs.php.net/bug.php?id=62119

If the first character is a non-ASCII character the character just disappears. If there are non-ASCII characters in the path itself it works fine.

Also what seems to be strange is that this problem only occurse for 64-bit PHP installations:

  • 64bit version (doesn't work as expected)
  • 32bit version (works as expected)

To solve this you could use a custom function as provided from a PHP manual comment:

function mb_basename($file) 
{ 
    return end(explode('/',$file)); 
} 

Code from: "(remove) dot nasretdinov at (remove) dot gmail dot com"


Use http://php.net/manual/en/function.setlocale.php to solve this problem.

setlocale(LC_CTYPE, 'fr_FR.utf8');

print basename('léquipe.jpg'); // léquipe.jpg
print basename('équipe.jpg');  // équipe.jpg

Try this (for windows server replace '/'):

function mb_basename($file)
{
  $temp = explode('/', $file);
  return end($temp);
}
print mb_basename('léquipe.jpg'); // léquipe.jpg
print mb_basename('équipe.jpg');  // équipe.jpg

Tags:

Php

Filenames