php exec()在unicode模式下?

cod*_*gel 9 php cmd exec utf-8

我需要执行命令行命令和工具,接受ut8作为输入或生成ut8输出.所以我使用cmd一个它的工作原理,但是当我从php用exec尝试这个时它不起作用.为了简单起见我尝试了简单的输出重定向.

当我在命令提示符下直接写:

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

创建了uft8.txt,内容是正确的.

цчшщюя-öüäß

当我使用php的exec函数时:

$cmd = "chcp 65001 > nul && echo ??????-öüäß>utf8.txt";
exec($cmd,$output,$return);
var_dump($cmd,$output,$return);
Run Code Online (Sandbox Code Playgroud)

utf8.txt中的内容搞砸了:

¥A¥Î¥^¥%¥ž¥?-ÇôǬÇÏÇY

我正在使用Win7,64bit和(控制台)代码页850.

我该怎么做才能解决这个问题?

其他信息:我试图克服在Windows上读取和写入utf8文件名的一些问题.PHP文件函数失败:glob,scandir,file_exists无法正确处理utf8文件名.文件是不可见的,跳过,名称被更改...因此我想避免PHP文件功能,我正在寻找一些PHP外部文件处理.

cod*_*gel 10

由于我找不到一个简单,快速和可靠的内部php解决方案,我结束使用我知道它的工作.CMD-批处理文件.我创建了一个在运行时生成cmd批处理文件的小函数.它只是预先设置chcp(更改代码页)命令以切换到unicode.并解析输出.

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;

}
Run Code Online (Sandbox Code Playgroud)

用法:就像php exec():

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

要么

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