dongzhuo1880 2018-08-26 00:19
浏览 994

PHP文件流数据以二进制形式返回

So I have a function that I CANNOT get to work. What's happening is it's coming back with data that's in binary and not the actual file. However, in the binary that I get back, the name of the particular file is included in the string. The data looks like this:

PK‹Mpt-BR/finalinport.html³)°S(ÉÈ,V¢‘ŒT…‚ü¢’ÒôÒÔâT…´Ì¼Ä ™” “I,QðT(ÏÌÉQ(-ÈÉOLQH„¨‡*Ê/B×]’X”žZ¢`£_`ÇPK.Ùô LePK‹M.Ùô Lept-BR/finalinport.htmlPKD

pt-BR is the directory and the 'finalinport.html' is the file that I am trying to have downloaded. If I replace the second parameter of fwrite to just a plain string, then everything works and I get the string that I wrote in a file inside of the zip. But not when I'm using Stream->getContents(), which leads me to believe that it is something going on with the stream. I cannot wrap my head around what can be happening. I've been on this for a week and a half now so any suggestions would be great.

public function downloadTranslations(Request $request, $id)
    {
        $target_locales = $request->input("target_locale");
        $has_source = $request->input("source");
        $client = new API(Auth::user()->access_token, ZKEnvHelper::env('API_URL', 'https://myaccount.com'));
        $document = Document::find($id);
        $job_document = JobDocument::where('document_id', $id)->first();
        $job = Job::find($job_document->job_id);

        $file = tempnam('tmp', 'zip');
        $zip = new ZipArchive();
        $zip->open($file, ZipArchive::OVERWRITE);
        $name_and_extension = explode('.', $document->name);

        if($target_locales == null){
            $target_locales = [];
            foreach ($job->target_languages as $target_language) {
                $target_locales[] = $target_language['locale'];
            }
        }

        foreach($target_locales as $target_locale){
            $translation = $client->downloadDocument($document->document_id, $target_locale);
            $filename = $name_and_extension[0] . ' (' . $target_locale . ').' . $name_and_extension[1];
            if($translation->get('message') == 'true') {
                //API brings back file in stream type
                $stream = Stream::factory($translation->get('body'));
                $newFile = tempnam(sys_get_temp_dir(), 'lingo');

                $handle = fopen($newFile, 'w');
                fwrite($handle, $stream->getContents());
                    $zip->addFile($newFile, 'eh.html');
                fclose($handle);
                }

                else if($translation->get('message') == 'false'){
                    //API brings back file contents
                    $zip->addFromString($filename, $translation->get('body'));
                }
            }
            $translation = $client->downloadDocument($document->document_id, null, null);

            $filename = $name_and_extension[0].  ' (Source).'.$name_and_extension[1];
            $zip->addFromString($filename, $translation->get('body'));
            sleep(10);
            $zip->close();

        return response()->download($file, $name_and_extension[0].'.zip')->deleteFileAfterSend(true);
}

I'm unfamiliar with PHP streams and I don't have a debugger set up so I keep thinking it has something to do with how I am handling the stream. Because the other condition (the else if) is coming back as content of the file (string) and the if statement, the data is coming back as a stream resource, which I am unfamiliar with.

  • 写回答

1条回答 默认 最新

  • dongyu1979 2018-08-26 08:43
    关注

    Stream::factory is used in Guzzle 5, use json_encode for Guzzle 6.

    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)