【发布时间】:2016-05-10 23:13:59
【问题描述】:
我正在使用 phantom 来捕获图像,然后将它们上传到一个集中的 MySQL 数据库。
我遇到的问题是它一直在切断我尝试推送的每个文件的结尾。不管它是 .pdf、.png、.docx、.xlsx ......它会切断文件的一部分,从而破坏它。奇怪的是它总是从每个文件中削减相同的数量。
我的 SQL 插入代码是:
$put_linkdata = $DBW->prepare('INSERT `'.$tableD.'` SET
captured = :capture_time,
error = :eYN,
link = :link,
image = :image,
html = :html,
`text` = :plaintext,
file = :file,
mimetype = :mimetype');
$put_linkdata->bindValue(':capture_time', date('Y-m-d H:i:s',$record['timestamp']));
$put_linkdata->bindValue(':link', $item['url']);
$put_linkdata->bindValue(':mimetype', $record['mimetype']);
$put_linkdata->bindParam(':eYN', $record['error'], PDO::PARAM_STR);
$put_linkdata->bindParam(':image', $record_child['image'], PDO::PARAM_LOB);
$put_linkdata->bindParam(':plaintext', $record_child['text'], PDO::PARAM_LOB);
$put_linkdata->bindParam(':html', $record_child['html'], PDO::PARAM_LOB);
$put_linkdata->bindParam(':file', $record_child['file'], PDO::PARAM_LOB);
if($put_linkdata->execute()){
$linkData_record['id'] = $DBW->lastInsertId();
print PHP_EOL.mb_strlen($record_child['image'], '8bit');
print PHP_EOL.PHP_EOL.'~~~~~~~~~~~~~~~~~~~~~~~~~~~'.PHP_EOL.PHP_EOL;
die();
}
else{
throw New Exception('Error inserting linkdata record into archive - SQL error "'.$put_linkdata->queryString.'"'.PHP_EOL.PHP_EOL.$put_linkdata->errorInfo());
}
我已经运行了大量的测试,在此之前的一切数据仍然保持完整性,如果我 file_put_contents() 它与捕获的图像或文件相同。
图片示例,拉 google.com 给我一个 25K 的图片,但上传的是 17.2K。 55K 的 pdf 以 40.7K 的速度上传。另一个 pdf 文件为 1.5 MB,但上传时为 1.1MB。当我将文件与 db blob 进行比较时,数据库中的 blob 缺少文件底部的内容。
文件的大小和内容不一致,但对于相同的捕获是一致的。
有人有什么想法吗?
更新 我已将其范围缩小到准备好的语句和上传之间发生的事情。
此外,当使用准备好的语句时,PDO 和 MySQLI 都会发生这种情况。第一个假设是某处的字符集编码不正确,但是所有内容都定义为 UTF8(数据库、表、PDO 连接、配置文件)。
【问题讨论】:
-
我的猜测是 BLOB 不足以容纳数据。尝试 MEDIUMBLOB 或 LONGBLOB。我之前回答过一个类似的问题,就是这样;列类型太小。
-
它实际上是一个 LONGBLOB 列,但我尝试将其更改为 MEDIUM 和 BLOB 以查看它是否与 PDO 和 blob 的大小有关。两种更改都没有奏效,即使是 BLOB,25K 仍然在该范围内。
-
脚本在完成 Blob 上传之前是否超时?
-
没有超时。在上传之前准备声明似乎有些问题。 $record_child['image'] 和 $record_child['file'] 变量已经是相同的数据流,就好像它们是 file_get_contents 一样。据我所知,无论是 PDO 还是 MySQLI,使用准备好的查询将任何 blob 对象传递给我的数据库都会导致它丢失数据。