PHP Read and render filenames (with special chars) for download












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.










share|improve this question

























  • 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 at 10:07











  • Possible duplicate of PHP output showing little black diamonds with a question mark

    – medunes
    Jan 20 at 10:09











  • As I've written, It is not showing a black diamond.

    – Emaborsa
    Jan 20 at 10:22
















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.










share|improve this question

























  • 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 at 10:07











  • Possible duplicate of PHP output showing little black diamonds with a question mark

    – medunes
    Jan 20 at 10:09











  • As I've written, It is not showing a black diamond.

    – Emaborsa
    Jan 20 at 10:22














0












0








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.










share|improve this question
















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.







php utf-8 special-characters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 11:19







Emaborsa

















asked Jan 20 at 9:55









EmaborsaEmaborsa

95331133




95331133













  • 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 at 10:07











  • Possible duplicate of PHP output showing little black diamonds with a question mark

    – medunes
    Jan 20 at 10:09











  • As I've written, It is not showing a black diamond.

    – Emaborsa
    Jan 20 at 10:22



















  • 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 at 10:07











  • Possible duplicate of PHP output showing little black diamonds with a question mark

    – medunes
    Jan 20 at 10:09











  • As I've written, It is not showing a black diamond.

    – Emaborsa
    Jan 20 at 10:22

















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 at 10:07





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 at 10:07













Possible duplicate of PHP output showing little black diamonds with a question mark

– medunes
Jan 20 at 10:09





Possible duplicate of PHP output showing little black diamonds with a question mark

– medunes
Jan 20 at 10:09













As I've written, It is not showing a black diamond.

– Emaborsa
Jan 20 at 10:22





As I've written, It is not showing a black diamond.

– Emaborsa
Jan 20 at 10:22












2 Answers
2






active

oldest

votes


















0














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






share|improve this answer
























  • Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1

    – Emaborsa
    Jan 20 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 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






share|improve this answer
























  • 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 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 at 11:37













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275287%2fphp-read-and-render-filenames-with-special-chars-for-download%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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






share|improve this answer
























  • Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1

    – Emaborsa
    Jan 20 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 at 17:36
















0














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






share|improve this answer
























  • Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1

    – Emaborsa
    Jan 20 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 at 17:36














0












0








0







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 20 at 10:01









wodkawodka

680415




680415













  • Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1

    – Emaborsa
    Jan 20 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 at 17:36



















  • Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1

    – Emaborsa
    Jan 20 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 at 17:36

















Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1

– Emaborsa
Jan 20 at 11:19





Both are on Windows, on localhost PHP is on 5.6.35 and on the server 7.1

– Emaborsa
Jan 20 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 at 17:36





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 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






share|improve this answer
























  • 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 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 at 11:37


















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






share|improve this answer
























  • 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 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 at 11:37
















0












0








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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 20 at 10:05









BrainmaniacBrainmaniac

8101518




8101518













  • 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 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 at 11:37





















  • 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 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 at 11:37



















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 at 10:25





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 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 at 11:37







> 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 at 11:37




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275287%2fphp-read-and-render-filenames-with-special-chars-for-download%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre