【问题标题】:Why PDF files stored in mysql database became damaged/corrupted when downloaded?为什么存储在 mysql 数据库中的 PDF 文件在下载时损坏/损坏?
【发布时间】:2012-03-31 13:49:08
【问题描述】:

我正在创建用户可以上传/下载存储在 mysql 数据库中的 pdf 文件的页面,当我下载文件时,它会损坏/损坏

NP:以blob形式存储在数据库中的文件数据。

下面是我的代码:

        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES           ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);

        // Create the SQL query
        $query = "
            INSERT INTO `file` (
                `name`, `mime`, `size`, `data`, `created`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW()
            )";

        // Execute the query
        $result = $dbLink->query($query);

        // Check if it was successfull
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}

download code: 
 // Fetch the file information
        $query = "
            SELECT `mime`, `name`, `size`, `data`
            FROM `file`
            WHERE `id` = {$id}";
        $result = $dbLink->query($query);

        if($result) {
            // Make sure the result is valid
            if($result->num_rows == 1) {
            // Get the row
                $row = mysqli_fetch_assoc($result);

                // Print headers
                //header('Content-Type: application/pdf');
                header("Content-Type: application/force-download"); 
                header("Pragma: public"); 
                header("Content-Description: File Transfer");
                header("Content-Type: ".$row['mime']);
                header("Content-Length: ".$row['size']);
                header("Content-Disposition: attachment; filename=".$row['name']);
                header("Content-Transfer-Encoding: binary");
                header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
                // Print data
                @readfile($row['data']);
            }
            else {
                echo 'Error! No file exists with that ID.';
            }

            // Free the mysqli resources
            @mysqli_free_result($result);
        }
        else {
            echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
        }
        @mysqli_close($dbLink);
    }
}
else {
    echo 'Error! No ID was passed.';
}

【问题讨论】:

标签: php mysql pdf blob


【解决方案1】:

不是这样。 mysqli 也不是办法。你总是有选择的,告诉你的老师这种方法没有价值。为什么不将 jpg 存储在 mysql 数据库中?因为有更好的方法 - 将链接存储在数据库中,将 .jpg 存储在网络上。与 PDF 相同。如果您必须仔细阅读,请查看 ckedit 等基于 Web 的编辑器如何存储数据(另一种错误方法。),并查看 dompdf code.google.com/p/dompdf/ 以了解破坏 pdf 格式的原因。

为什么 mysqli 是个坏主意? mySql 就是这样。 mySql 尝试用其他方法更好地完成它,否则 mysql 会这样做。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-14
    • 2017-07-08
    相关资源
    最近更新 更多