尝试通过PHP / MySQL下载Blob [英] Trying to download Blob via PHP / MySQL

查看:95
本文介绍了尝试通过PHP / MySQL下载Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我无法想象的:我已经在网页上成功构建了一个上传功能,将文件上传到MySQL数据库。当我去服务器并通过MyPHPAdmin打开它们时,他们都看起来很好... txt,jpg,pdf等。

This is one I just can't figure out: I have successfully built an upload feature on a web page to upload files to a MySQL Database. When I go on the server and open them via MyPHPAdmin, they all look fine... txt, jpg, pdf, etc.

然而,把这个东西下载),我得到一个奇怪的问题:所有的文本文档(和所有其他类型的文件,我将扩展名更改为txt后)包含页面本身的HTML代码,其次是原始内容!

Yet, after putting together THIS thing (below) to download it, I get a strange problem: All of the text documents (and all other types of files, after I change the extension to 'txt') contain HTML code of the page itself, followed by the original content!

此外,不同的浏览器在POST后显示不同。当尝试下载txt文件时,IE会在页面上显示ECHO中的正确数据(无下载),并显示错误消息:警告:标题可能不包含多个单个标题,新行检测。第82行是C:\wamp\www\ace\dmain.php。第82行是'header(Content-length ...

Also, different browsers display differently after the POST. When trying to download a txt file, IE will show the correct data in the ECHO on the page itself (no downloading) with an error message just before it: Warning: Header may not contain more than a single header, new line detected. in C:\wamp\www\ace\dmain.php on line 82. Line 82 is 'header("Content-length...'

Firefox和Chrome都不显示任何东西,只允许我下载它。

Neither Firefox nor Chrome show anything. They just allow me to download it.

这是代码,希望有人可以看出这一点:

Here's the code. Hopefully someone can shed light on this:

<?php
if (isset($_POST['downloadid'])) {
    $fileid = $_POST['downloadid'];
    try {
      $sql = "SELECT * FROM `datastore` WHERE `id` = '".$fileid."'";
        $results = $pdo->query($sql);echo $sql;
        while ($row = $results->fetch()) {
            $filename = $row['filename'];
            $mimetype = $row['mimetype'];
            $filedata = $row['filedata'];
            header("Content-length: strlen($filedata)");
            header("Content-type: $mimetype");
            header("Content-disposition: download; filename=$filename"); //disposition of download forces a download
            echo $filedata; 
            // die();
        } //of While
    } //try
    catch (PDOException $e) {
        $error = '<br>Database ERROR fetching requested file.';
        echo $error;
        die();    
    } //catch
} //isset
?>


推荐答案

这个:

header("Content-length: strlen($filedata)");

不会产生你的期望。如果您查看wireshark中的标题,或查看请求的其他方法,您将看到它不包含一个整数。

Is not going to produce what you expect. If you look at the headers in wireshark, or another method to view the request you will see that it does not contain an integer.

使用此代替:

header("Content-length: ".strlen($filedata));

这篇关于尝试通过PHP / MySQL下载Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆