dongzan7016 2019-06-20 15:18
浏览 51
已采纳

如何在下载文件时修复奇怪的文件编码?

I am trying to download a file from postgresql encoded in base 64. The content of the file seems to be good in the database bytea but it is not the same when I download it with PHP:

I am using PHP 5.6 with symfony 2.8. I've tried to decode in base 64 but with no success.

$name = $file->getName();
$content = base64_decode(stream_get_contents($file->getContent()));
$contentType = $file->getContentType();
$response = new Response();
$response->headers->set("Cache-Control", "no-cache private");
$response->headers->set("Content-Description", "File Transfer");
$response->headers->set('Content-Type', $contentType);
$response->headers->set('Content-Disposition', 'attachment; filename="' . $name . '"');
$response->headers->set("Content-Transfer-Encoding", "binary");
$response->headers->set('Content-Length: ', strlen($content));
$response->sendHeaders();
$response->setContent($content);
return $response;

For this example, I've created a file with notepad++ containing the string "test". In the database it is displayed like this "dGVzdA==".

When I download the file and I decode it, the content is this "Ǯ8kθطw", it should be "test".

  • 写回答

1条回答 默认 最新

  • dongzaliang4492 2019-06-20 16:47
    关注

    PGSQL is converting my file into a hexadecimal value. It needed to be decoded. Moreover, there is a "/" at the beginning of the PQGSQL response. It needs to be removed.

    Here is my treatment to the content:

    $content = base64_decode(hex2bin(
        substr(
            stream_get_contents(
                $file->getContent()
            )
        ,1)
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题