0

I have to render a list of files, with link to them. What I have written:

$path = "pdf/".$lang."/*";
$fileList = glob($path);

echo "<ul>";
foreach($fileList as $filename){
    $splitted = explode("/", $filename);
    echo '<li><a href="'.$filename.'">'.end($splitted).'</a>'; 
}
echo "</ul>";

It works if there are no special chars in the filenames. If there are, a � is rendered.

I changed the code followings:

$path = "pdf/".$lang."/";
$fileList = glob($path."*");

echo "<ul>";
foreach($fileList as $entry){
    $splitted = explode("/", $entry);
    $filename = iconv('WINDOWS-1252', 'UTF-8', end($splitted));
    echo '<li><a href="'.$path.$filename.'" target="_blank">'.$filename.'</a>'; 
}
echo "</ul>";

this solution works only on the localhost. Once I publish it the special chars are not rendered correctly. Instead of the ä it renders ä. How can I solve it?

Edit:
This is not a duplicate of this question. As I'written above I get ä not �.

Localhost and server are working on Windows. PHP Version on localhost is 5.6.35. Server in on 7.1.

Emaborsa
  • 2,360
  • 4
  • 28
  • 50
  • Are you on Windows? What's your PHP version? That was a traditional problem in PHP on Windows but it was fixed on PHP/7.1. – Álvaro González Jan 20 '19 at 10:07
  • Possible duplicate of [PHP output showing little black diamonds with a question mark](https://stackoverflow.com/questions/275411/php-output-showing-little-black-diamonds-with-a-question-mark) – medunes Jan 20 '19 at 10:09
  • As I've written, It is not showing a black diamond. – Emaborsa Jan 20 '19 at 10:22

2 Answers2

0

It looks like the filename is then encoded twice with UTF-8 - what encoding settings are you using on your server / local php?

wodka
  • 1,320
  • 10
  • 20
  • Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1 – Emaborsa Jan 20 '19 at 11:19
  • ok :) now I'm pretty sure it is just a double utf encoding - have you tried outputting it without any further encoding on your server? – wodka Jan 20 '19 at 17:36
0

I am not absolutely sure but this might be that your file on server gets encoded to ISO-8859-1 instead of utf-8.

please see this question as a ref: Swedish characters and UTF-8

I wrote an answer on this issue, it is the second answer.

Try changing your charset meta-tag to ISO-8859-1

Brainmaniac
  • 2,203
  • 4
  • 29
  • 53
  • no, I used the html5 notation for setitng UTF8. However, if this were the problem, shouldn't it behave the same on localost and on the server? – Emaborsa Jan 20 '19 at 10:25
  • > However, if this were the problem, shouldn't it behave the same on localost and on the server? - Probably but not 100%. Might be some server encoding going on somewhere that we forget to think about – Brainmaniac Jan 20 '19 at 11:37