【问题标题】:PDF not downloading when file is called调用文件时PDF不下载
【发布时间】:2016-01-10 08:48:01
【问题描述】:

我有一个数据库,用于存储网站的所有 pdf 文件。该表包含 library_item_id、文件名(文件名)、mime_type、文件大小、file_item(Blob)的列,我有一个名为 download.php 的 php 文件。当用户单击链接时,该文件应该从数据库中下载正确的文件。但是当文件被下载并单击打开时,我得到 Adob​​e 说它无法打开 pdf。说它没有正确解码。这是我的 download.php 文件:

require_once("scripts/connection.php");
    if(isset($_GET["id"])){
        $fid = $_GET["id"];
    }
    else{
        header("Location: literature.php"); 
    }

    $sql= "SELECT filename, mime_type, file_item FROM library_items WHERE library_item_id = ?"; 
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('s', $fid);
    $stmt->execute();
    $stmt->bind_result($filename, $mime, $file_item);

    while($stmt->fetch()){
        $file_name = $filename;
        $mime_type = $mime;
        $file = $file_item; 
    }

    header("Content-length: ".strlen($file));
    header("Content-type: $mime_type");
    header("Content-disposition: download; filename=$file_name"); 
    echo $file;

    mysqli_close($conn);

我已经尝试了所有我能想到的方法,包括添加 obj_flush() 命令,但它仍然给我同样的错误。我在这方面做错了什么?

这是将文件插入数据库的代码的编辑。

session_start();

    $display = trim($_POST["file-display-name"]);
    $company = trim($_POST["companies"]);
    $lib_cat = trim($_POST["library-cats"]);

    if(empty($display) || empty($company) || empty($lib_cat)){
        $_SESSION["errormsg"] = "Required information is missing please fill out all required fields.";
        header("Location: ../library.php");
    }
    else{
        $file_name = $_FILES['library-file']['name'];
        $tmp_name = $_FILES['library-file']['tmp_name'];
        $file_size = $_FILES['library-file']['size'];
        $file_type = $_FILES['library-file']['type'];

        $fp = fopen($tmp_name, 'r');
        $content = fread($fp, filesize($tmp_name));
        $content = addslashes($content);
        fclose($fp);

        if(!get_magic_quotes_gpc()){
            $file_name = addslashes($file_name);    
        }

        if(empty($content)){
            $_SESSION["errormsg"] = "Required information is missing please fill out all required fields.";
            header("Location: ../library.php"); 
        }
        else{   
            require_once("connection.php");

            // Insert the logo into the companies photo table
            $sql = "INSERT INTO library_items(filename, mime_type, file_size, file_item, display_name, company_id, library_category_id) VALUES(?,?,?,?,?,?,?)";

            $stmt = $conn->prepare($sql);
            $stmt->bind_param('sssssss', $file_name, $file_type, $file_size, $content, $display, $company, $lib_cat);
            if(!$stmt->execute()){
                $_SESSION["errormsg"] = "Failed to add library item: ".mysqli_error();
                header("Location: ../library.php");
            }
        }
        unset($_SESSION["errormsg"]);

        $_SESSION["successmsg"] = "Library Item successfully added into the database.";
        header("Location: ../library.php");
    }

更新: 我现在下载文件并尝试在双击打开下载的文件后显示。它告诉我有一个无效的颜色空间。据我所知,这是将文件上传到数据库时出现的问题。从我发布的上传文件中,有什么我做错了吗?

【问题讨论】:

  • 你的表结构是什么?看来您的 file_item 列的类型有误。
  • @Alex : 你需要下载吗?
  • file_item 是一个大块。这实际上是文件的内容。
  • 是的,我需要文件下载或显示在一个或另一个页面上。
  • @AlexBeyer :试试我编辑的答案。抱歉,我没有测试过。

标签: php mysql pdf


【解决方案1】:

您需要进行以下更改。在插入文件中 而不是

$content = addslashes($content);

使用

$content = base64_encode($content);

因此,在下载时,对其进行解码。

$file = base64_decode($file_item);

替换以下代码

header("Content-type: '".$mime_type."'");
header("Content-Disposition: inline; filename='".$file_name."'");
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
readfile($file);

header("Content-length: ".strlen($file));
header("Content-type: $mime_type");
header("Content-disposition: download; filename=$file_name"); 
echo $file;

【讨论】:

  • 如果我使用它,它不会给我无法加载 pdf,但文件会打开,并且它实际上是一个像 pdf 一样的空白矩形,但没有添加任何内容。 ://
  • 如果我尝试从下载的文件中打开它,它还会告诉我它的颜色空间无效?我以前从未见过。
  • 是我将文件上传到数据库的问题吗?从我对这个错误的发现来看,它与文件未正确保存有关,但我看不到将文件上传到数据库时出错的地方。
  • 使用base64_encode 而不是addslashes @AlexBeyer
  • 甜蜜就是问题所在!谢谢,我忘记了编码。
【解决方案2】:

检查下载文件的大小是否正确(与数据库中的文件大小匹配)。如果是,您应该调试文件插入数据库的部分。如果没有 - 在将文件发送到浏览器之前检查是否打印出任何内容 - 也许scripts/connection.php 会打印空行、错误消息或 BOM 之类的东西? 如果找不到,请尝试打开输出缓冲并在发送文件之前调用ob_clean()

对不起我的英语,我希望它是可以理解的。

【讨论】:

  • 我看这里应该没有打印出来的东西。我也会将我的插入代码作为编辑发布。
  • 对不起,我完全错了。您不能将 readfile 与字符串一起使用,因为它不是文件。请改用echo
猜你喜欢
  • 2011-11-26
  • 1970-01-01
  • 2016-12-28
  • 2015-08-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多