The commenting and the another answer are mistaken, it has no sense to force encoding in this case, you can even choose to use iso-8859-1 or utf-8 in your page’s HTTP response, but simply what the exec()
returns is not UTF-8 or windows-1252 (or equivalent) in its encoding, it probably returns some format of the "OEM code page" and in PHP for Windows servers specifically there are already functions to handle this since the PHP 7, are they:
Basically you will use this to catch the number that represents the oem codepage:
$oem = sapi_windows_cp_get('oem');
And will use so to convert in the desired codepage, the Windows equivalent for UTF8 is codepage 65001
$var = sapi_windows_cp_conv($oem, 65001, $var);
In your code you can get something like:
<?php
exec('dir "C:\Users\new_g\Desktop\aaa" /b', $output);
$codepage = sapi_windows_cp_get('oem');
foreach ($output as $file) {
$file = sapi_windows_cp_conv($codepage, 65001, $file);
echo $file, "<br>";
}
Remember that AFTER using sapi_windows_cp_conv
and as we chose 65001, we must set the charset to UTF-8, can do this via Apache, Ngnix, or other server you are using, or direct in PHP with header
at the top:
header('Content-Type: text/html; charset=UTF-8');
But if on your pages you need/want to use iso-8859-1 you need then, AFTER using the sapi_windows_cp_conv
, use the utf8_decode
(to DECODE UTF8) and also set the header to iso-8859-1
<?php
header('Content-Type: text/html; charset=iso-8859-1');
exec('dir "C:\Users\new_g\Desktop\aaa" /b', $output);
$codepage = sapi_windows_cp_get('oem');
foreach ($output as $file) {
$file = sapi_windows_cp_conv($codepage, 65001, $file);
$file = utf8_decode($file);
echo $file, "<br>";
}
Don’t use exec if you don’t need to
Now I must be honest, I have no idea why you decided to use the command dir "C:\Users" /b
with exec()
, the advantages are:
- You won’t need to keep tweaking codepage
- Will run on php5 (depends on the rest of your codes)
- It will be compatible with all systems where PHP is supported (Windows and Linux for example)
It has several functions for this, such as glob()
, opendir()
(reasonably faster and can provide good manipulator control by being able to create a page that would hardly consume any memory other than those that use array, but only if it is a folder with thousands of files will notice some improvement) and scandir()
, this last one is simple and easy for you to apply, getting like this:
$dir = 'C:/Users';
foreach (scandir($dir) as $file) {
//Ignora as pastas . e .. que são navegações
if ($file != '.' && $file != '..') {
echo $file, "<br>";
}
}
And as well remembered by his colleague @Bacchus, all on the assumption that the files are correct for the filesystem in question.
Use the
utf8_encode($text)
– Jakson Fischer
It’s still the same
– João Victor
Use in this way
<?php header ('Content-type: text/html; charset=UTF-8'); ?>
If you are using ISO, simply change theUTF-8
forISO-8859-1
. Exchange the returns and shipments to the bank forUTF-8
orISO-8859-1
also.– Jakson Fischer