9

I need to execute command line commands and tools that accept ut8 as input or generate an ut8 output. So i use cmd an it works, but when i try this from php with exec it doesn't work. To make it simple i tried simple output redirection.

When i write direct in command prompt:

chcp 65001 > nul && echo цчшщюя-öüäß>utf8.txt

The uft8.txt is created an the content is correct.

цчшщюя-öüäß

When i use the exec function from php:

$cmd = "chcp 65001 > nul && echo цчшщюя-öüäß>utf8.txt";
exec($cmd,$output,$return);
var_dump($cmd,$output,$return);

the content in the utf8.txt is messed up:

¥Å¥Î¥^¥%¥Z¥?-ÇôǬÇÏÇY

I am using Win7,64bit with (Console) Codepage 850.

What should i do to fix that?

Additional Infos: I am trying to overcome some issues with reading and writing of utf8 filenames on windows. PHP file functions fail: glob, scandir, file_exists can't handle utf8 filenames correctly. File are invisible, skipped, names are changed ... Therefore i want to avoid php file functions and i am looking for some php extern filehandling.

3
  • one easy solution should be to pipe the chars with php.net/fwrite to write to file Commented Nov 11, 2012 at 15:03
  • i can write the string direct at once with file_put_contents, but that is not the point. My example is a only a simple representation of the problem. I added Additional Infos to my question Commented Nov 11, 2012 at 16:02
  • Well, php does not support utf-8, or any other charset, so you might be trying impossible things. I don’t know how windows handles charsets for console applications, so I can’t help with that one. Commented Nov 11, 2012 at 16:31

1 Answer 1

10

Since i couldn't find an easy, fast and reliable internal php solution, i am ending with using that i know it's work. Cmd-Batch-File. I make a small function that generate a cmd batch file in runtime. Its just prepends the the chcp (change the codepage) command in order to switch to unicode. And parse the output.

function uft8_exec($cmd,&$output=null,&$return=null)
{
    //get current work directory
    $cd = getcwd();

    // on multilines commands the line should be ended with "\r\n"
    // otherwise if unicode text is there, parsing errors may occur
    $cmd = "@echo off
    @chcp 65001 > nul
    @cd \"$cd\"
    ".$cmd;


    //create a temporary cmd-batch-file
    //need to be extended with unique generic tempnames
    $tempfile = 'php_exec.bat';
    file_put_contents($tempfile,$cmd);

    //execute the batch
    exec("start /b ".$tempfile,$output,$return);

    // get rid of the last two lin of the output: an empty and a prompt
    array_pop($output);
    array_pop($output);

    //if only one line output, return only the extracted value
    if(count($output) == 1)
    {
        $output = $output[0];
    }

    //delete the batch-tempfile
    unlink($tempfile);

    return $output;

}

Usage: just like php exec():

utf8_exec('echo цчшщюя-öüäß>utf8.txt');

OR

uft8_exec('echo цчшщюя-öüäß',$output,$return);

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like this might actually work for me. I need to create/rename files in Windows with Unicode characters and so far, this is the only solution that actually managed to do it at all. I just need to work out writing-to-non-existent-pipe issue, and then I will finally be able to finish this project. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.