【发布时间】:2017-09-05 00:31:50
【问题描述】:
我正在使用来自 Gmail API 的 base64 字符串下载附件。当我使用 Windows 打开下载的文件时,我看到错误 'We can't open this file'。我检查了 $data 数组中的标头,它们是正确的,我还检查了下载文件的大小,这也是正确的大小。
我使用以下方式下载文件:
$data = $json['data'];
$data = strtr($data, array('-' => '+', '_' => '/'));
$image = base64_decode($data);
header('Content-Type: image/jpg; name="crop-1.jpg"');
header('Content-Disposition: attachment; filename="crop-1.jpg"');
header('Content-Transfer-Encoding: base64');
header('X-Attachment-Id: f_j1bj7er60');
readfile($image);
// I have also tried
echo $image;
$image 字符串是有效的,因为如果我使用下面的图像正确显示:
echo "<div>
<img src=\"data:image/jpg;base64, $image\" />
</div>";
如何修复文件下载?
【问题讨论】:
标签: php base64 gmail-api readfile