Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

basename() fail when file name start by an accent

Tags:

php

filenames

Here is a quick example to show the issue:

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

How can I avoid this inconsistency, and be sure that basename() will return me the whole file name?

Thanks

like image 511
zessx Avatar asked Aug 20 '15 10:08

zessx


2 Answers

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"

like image 85
Rizier123 Avatar answered Sep 20 '22 20:09

Rizier123


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
like image 41
Bell Avatar answered Sep 18 '22 20:09

Bell