【问题标题】:Downloading 'blob' type file from database with PHP. Files are corrupted, unable to see the files' content使用 PHP 从数据库下载“blob”类型文件。文件已损坏,无法查看文件内容
【发布时间】:2021-12-12 03:12:59
【问题描述】:

我尝试使用此代码从我的数据库中下载文件,但是,当我尝试打开文件时,出现以下错误:

下载文件类型“图像”时:

下载文件类型“docx”时:

php代码:

<?php
        include_once 'dbconfig.php';
        if (isset($_GET['id'])) {
            $id = $_GET['id'];
            $query = "SELECT * FROM files_data WHERE id = '$id'";
            $result = mysqli_query($connection,$query) or die('Error, query failed');
            list($id, $file, $type, $size,$content) = mysqli_fetch_array($result);
            header("Content-length: $size");
            header("Content-type: $type");
            header("Content-Disposition: attachment; filename=$file");
            $content = stripslashes($content);
            echo file_get_contents($content);
            exit;
        }
    ?>

数据库:

Word 文档的存储类型为:“application/vnd.openxmlformats-officedocument.wordprocessingml.document”(在屏幕截图中不可见)。我相信这是正确的

【问题讨论】:

标签: php download blob


【解决方案1】:

我在尝试一些不同的代码时解决了这个错误:

<?php require_once('dbconfig.php'); ?>
<?php
   $query = "SELECT name, type, size, content from files_data WHERE id='2'";

   $result = $connection->query($query);
   $row = $result->fetch_assoc();
   $size = $row['size'];
   $type = $row['type'];
   $name =$row['name'];
   $fileContent = $row['content'];

   header("Content-length: $size");
   header("Content-type: $type");
   header("Content-Disposition: attachment; filename=$name");
   echo $fileContent;
   mysql_close();
?>

另外,我仍然认为我以前的代码很好,也许我遇到了与这里报告的相同的问题,答案是:Downloading jpg,doc,ppt files with php are corrupted

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多