1

I am doing this:

$files = scandir('./data');
foreach($files as $file) {
    echo $file;
}

I have also tried this:

foreach (new DirectoryIterator('./music') as $fileInfo) {
    echo $fileInfo->getFilename();
}

It works fine for most file names, except for those with special characters, such as é or ø. The output will contain black diamonds with question marks (�). I have tried a lot of different methods for converting from and to UTF-8, UTF-16, ISO-8859-1, etc. Eventually I realised that php must be outputting a �, instead of the browser displaying it because it doesn't know what the character is.

How do I properly get the name of a file with special characters?

It would be okay if e.g. éø is converted to eo, as long as I do not lose characters (géu -> gu)

Derkades
  • 121
  • 1
  • 6
  • Made this gist for helping you replacing accents https://gist.github.com/halesyy/1e8539e4314d2761f48211168786e642 – Jack Hales Jan 10 '17 at 09:45
  • In a nutshell: you need to be exactly aware of what file system you're using, how it encodes file names, how PHP gets to see those file names, and how you need to convert the data accordingly. It's messy. It's terrible. It's a recipe for disaster. Yes, even something so trivial is still not a solved problem in this day and age. (PHP isn't alone in this.) – deceze Jan 10 '17 at 09:50
  • Why is this a duplicate? That other question is about making a directory using php, and making sure it does not show up weird when viewed in Windows Explorer. My question is about files that already exist; getting the name of those files. I cannot simply change those file names. – Derkades Jan 10 '17 at 09:55
  • But if it's near impossible or very hard, I may need to look into different solutions. – Derkades Jan 10 '17 at 09:57
  • I've closed it as a dupe for now because that dupe contains a lot of information about the issue which may lead you to the solution. If not, you'll have to provide more details about your specific file system/OS/encoding for us to be able to help you in a reasonable scope. – deceze Jan 10 '17 at 10:49
  • I ended up using https://github.com/kenjiuno/php-wfio . Thanks for the help though! – Derkades Jan 10 '17 at 14:39

0 Answers0