PHP Exec Returning unknown characters when executing commands

Asked

Viewed 126 times

-1

I am running functions in Windows CMD through PHP in Exec function, but some returned values come as unknown character, mainly in accents, how to proceed to fix? Commando:

exec('dir "C:\Users" /b', $x);

Upshot:

inserir a descrição da imagem aqui

In the output is returning me an Array but either giving a var_dump or print_f the values are as in the image.

  • Use the utf8_encode($text)

  • It’s still the same

  • Use in this way <?php header ('Content-type: text/html; charset=UTF-8'); ?> If you are using ISO, simply change the UTF-8 for ISO-8859-1. Exchange the returns and shipments to the bank for UTF-8 or ISO-8859-1 also.

2 answers

4


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.

  • It worked your solution, thank you very much, but the reason I use directly by exec was because as it comes from cmd, it will not show specific folders and files, such as swapfile.sys, System Volume Information, $Recycle.Bin and so on, since the purpose of the application I’m developing is to navigate through any part of Windows like Explorer, it’s certain that the PHP commands themselves are much faster, but leave something to be desired in some points using Windows, and it can get tiring fill of exceptions in the code.

  • @Joãovictor understood, actually some things of Windows are so specific that it is difficult to pass to PHP or even other languages, because they would depend on the windows32 API and as on other systems hidden files do not work in the same way as on windows then it has to be something specific, maybe even with the class COM PHP could, but the complication would be so much that really in this case it is better exec

-2

uses utf8_string_array_encode($array) with your array.

function utf8_string_array_encode(&$array){
    $func = function(&$value,&$key){
        if(is_string($value)){
            $value = utf8_encode($value);
        }
        if(is_string($key)){
            $key = utf8_encode($key);
        }
        if(is_array($value)){
            utf8_string_array_encode($value);
        }
    };
    array_walk($array,$func);
    return $array;
}

https://www.php.net/manual/en/function.utf8-encode.php

  • Changed, but now instead of the "A" appears "Æ"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.