PHP base64_encode PDF文件已损坏

问题描述 投票:1回答:1

我有一个自动PHP脚本,它连接到一个电子邮箱,读取电子邮件并处理它们以创建票证。其中一些电子邮件包含各种类型的文件附件。我的脚本使用以下代码将文件直接保存到postgress数据库。我正在使用codeigniter。

public function saveFiles($filename, $fileurl, $jobid) {
     $filedata = array();
     $filedata['filename']= $filename;
     $filedata['filedescription'] = 'Incoming attachment.';
     $filedata['fileargid'] = $jobid;
     $filedata['fileaddedon'] = date('Y-m-d H:i:s P');
     $filedata['filedata'] = pg_escape_bytea(base64_encode(file_get_contents($fileurl)));

     $results = $this->db->insert('file', $filedata);
     if ($results)
         return $this->db->insert_id();
     else
         return FALSE;
}

但是,大多数文件都保存没有任何问题。我的问题是当我部署这个脚本时,一些pdf文件被破坏了。脚本在编码到base64之前将文件保存到本地磁盘。所有这些文件都是健康的。我怀疑在pg_escape_bytea(base64_encode(file_get_contents($fileurl)))期间发生了一些事情。

我在我的本地PC上使用php 5.5.9 / Ubuntu开发了这个脚本,并且没有文件在那里被破坏。但是脚本部署在带有5.3.5的Ubuntu服务器上,文件在那里被破坏了。

我试图找出导致这种情况的原因但到目前为止没有锁定。这是因为不同的php版本?

php postgresql base64 email-attachments php-imap
1个回答
1
投票

看起来:

  1. 你以“转义”格式编码到数据库并以十六进制格式读取它。
  2. 您需要强制转换“当客户端和后端字符编码不匹配时,可能存在多字节流错误。用户必须转换为bytea以避免此错误。”来自pg_escape_bytea documentation,它也算不上风景。

检查第8.1节here

如果不是问题,我会直接将bin2hex输出保存到字段中。

© www.soinside.com 2019 - 2024. All rights reserved.