【问题标题】:PHP LongBlob Upload/Download not working with picturesPHP LongBlob上传/下载不适用于图片
【发布时间】:2017-11-07 11:45:21
【问题描述】:

我必须在我的数据库中存储大量文档,并且我设法使它与 PDF 一起使用。 例如,当我尝试上传 JPG 或 GIF 时,当我从下面的下载功能重新下载时,内容没有显示。

下载功能:

    public function downloadFile($id) {
    if($id!=null){
    $sql = "SELECT * FROM document WHERE id = ?";
    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(1, $id);
    $stmt->execute();
    foreach ($stmt as $row) {
      header("Content-length:".$row['taillefichier']);
      header("Content-type:" . $row['typefichier']);
      header("Content-Disposition: attachment; filename=".$row['nomfichier']);
      echo $row['fichier'];
    }
  }

【问题讨论】:

  • “例如,当我尝试上传 JPG 或 GIF 时,重新下载时内容没有显示。” - 问题是;是否有任何内容上传到 db 开始?你也在检查错误吗?
  • ...如果有内容发送到数据库,与文件源相比大小是多少?
  • 是的,我可以从 MySQL Blob 字段下载它,但是我的 PHP 下载功能似乎不起作用,使用下载功能后尝试打开它时总是显示内容错误,大小MySQL表的size字段为209942,blob字段为205000
  • 查看php.net/manual/en/function.error-reporting.phpphp.net/manual/en/pdo.error-handling.php 以及我在上面留下的其他评论是否有任何内容
  • 我已经编辑了我的评论答案!错误报告似乎没有向我展示特别的东西

标签: php file png blob gif


【解决方案1】:

所以我找到了我自己问题的答案,我会在这里发布更新的功能

    public function downloadFile($id) {
    if($id!=null){
    $sql = "SELECT * FROM document WHERE id = ?";
    $stmt = $this->db->prepare($sql);
    $stmt->bindValue(1, $id);
    $stmt->execute();
    foreach ($stmt as $row) {
      header("Content-length:".$row['taillefichier']);
      header("Content-type:".$row['typefichier']);
      header("Content-Disposition: attachment; filename=".$row['nomfichier']);
      header("Content-type:".$row['typefichier']);
      ob_clean();
      flush();
      echo $row['fichier'];
    }
  }
}

在此处查看更多信息:https://stackoverflow.com/a/31318718/7295677

【讨论】:

    猜你喜欢
    • 2012-08-01
    • 2018-06-16
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-01
    相关资源
    最近更新 更多