我正在开发一个项目,我将文件直接存储在MySQL数据库中。代码工作得很好,存储和下载文本文件,图片和PDF,但是当使用Excel电子表格时,它有一个问题。
如果我存储Excel传播(.xlsx)文件,上传工作正常,但是当我下载文件时,下载似乎工作正常,但是当我打开电子表格时,我得到以下内容:
$crawler2 = $client->followRedirect();
当我按We found a problem with some content in 'filename.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbench, click Yes?'
时,我会得到以下内容:
Yes
执行此操作时,Excel加载正常,一切看起来都不错,但为什么我会收到此错误,为什么需要修复它。
我使用以下内容存储文件:
Excel completed file level validation and repair. Some parts of this workbook may have been repaired or discarded.
我使用以下内容下载文件:
private function writeFileToDatabase($fileName, $tmpName, $type, $fileSize, $fileArea, &$error)
{
$fileName = mysqli_escape_string($this->conn, $fileName);
$fileSize = mysqli_escape_string($this->conn, $fileSize);
$type = mysqli_escape_string($this->conn, $type);
$fileArea = mysqli_escape_string($this->conn, $fileArea);
//Open the uploaded file
$fh = fopen($tmpName, 'r');
if (!$fh)
{
$error = "Could not open uploaded file";
return false;
}
$content = fread($fh, filesize($tmpName));
$content = mysqli_escape_string($this->conn, $content);
fclose($fh);
//Store the file in the database
$query = "INSERT INTO file_store (RecordID, Area, FileName, ContentType, Size, FileContent) "
. "VALUES ('$this->recordID', '$fileArea', '$fileName', '$type', '$fileSize', '$content')";
$result = $this->conn->query($query);
}
答案 0 :(得分:1)
之后
echo $data["FileContent"];
尝试添加此内容:
ob_clean();
flush();
如果仍然无效 - 可能会尝试捕获内容类型,如果不是文本文件则添加另一个标题:
header('Content-Transfer-Encoding: binary');