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


Most of the core PHP functions don’t deal with character sets apart from Latin-1. But before the ‘pathinfo’, placing the ‘setlocale’ can be used to return the correct filename even if it is UTF-8 encoded.

By default, it runs with ‘C’ locale, and CLI scripts run with a default utf-8 locale. The locale on the server should be changed from ‘C’ to ‘C.UTF-8’ or to ‘en_US.UTF-8’ before calling the other functions.

setlocale(LC_ALL,'en_US.UTF-8');
pathinfo($OriginalName, PATHINFO_FILENAME);
pathinfo($OriginalName, PATHINFO_BASENAME);

Updated on: 09-Apr-2020

188 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements