Displaying BLOB PDF File

I have some pdf files that are stored in the wordpress database as a blob and I need to display these files. The files are not linked to any posts directly.

The code I have currently is:

Read More
<div class="wrap">
<?php
global $wpdb;

$mydataset = $wpdb->get_row("SELECT * FROM wp_attachments WHERE ID = 1119");

$recordID = $mydataset->ID;
$FileType = $mydataset->FileType;
$FileSize = $mydataset->FileSize;
$FileName = $mydataset->FileName;
$FileContent = $mydataset->FileContent;

add_action( 'send_headers', 'add_header_info' );
function add_header_info() {
header("Content-Type: ". $FileType);
header("Content-Length: ". $FileSize);
header("Content-Disposition: attachment; filename=". $FileName);
echo $FileContent;
}

 echo $FileContent;
?>

The problem is that the I do not get the normal open/save file dialogue box from the browser, but rather I get a whole lot of:

%PDF-1.5 %���� 21 0 obj <> endobj 36 0 obj <>/Filter/FlateDecode
/ID[<3D64A2F9453F2A4097AB5FEE91CB4D65>]/Index[21 22]/Info 20 0 R/Length 78/Prev  
58282/Root 22 0 R/Size 43/Type/XRef/W[1 2 1]>>stream h�bbdb�$��V �"�@�M��k�x
"n���A�d��R �HLfbdXcD'�3���a  endstream endobj startxref 0 %%EOF 42 0 obj 
<>stream h�bc2df`H�g�a@& �x�(�x���9X����YH�1
X������CC�C���A�As�d�+,��$��p00��iF �bn��&�� ��q� endstream 
endobj 22 0 obj <> endobj 23 0 obj <> endobj 24 0 obj <>stream hތS�n�@��yLT�{�
Ɏc�m�X�mĬ)*�����,�-

Can anyone please advise me on how I can correctly display the BLOB data?

Related posts

1 comment

  1. That looks like it is a template file. If so, the function add_header_info is hooked into send_headers long after send_headers had fired. You will need to move that code so that it executes earlier. It should work from a mu-plugin file, a plugin file or from your theme’s functions.php.

    However, $FileType and those other variables are going to be out of scope. (They are out of scope in your code already.) It looks to me like you can fix that by moving all of the code inside the callback.

    function add_header_info() {
      global $wpdb, $FileContent;
    
      $mydataset = $wpdb->get_row("SELECT * FROM wp_attachments WHERE ID = 1119");
    
      $recordID = $mydataset->ID;
      $FileType = $mydataset->FileType;
      $FileSize = $mydataset->FileSize;
      $FileName = $mydataset->FileName;
      $FileContent = $mydataset->FileContent;
    
      header("Content-Type: ". $FileType);
      header("Content-Length: ". $FileSize);
      header("Content-Disposition: attachment; filename=". $FileName);
    }
    add_action( 'send_headers', 'add_header_info' );
    

    Use the following in your template to echo the file contents:

    global $FileContent;
    echo $FileContent;
    
    // or 
    
    // echo $GLOBAL[$FileContent];
    

    But that will run on every page which would be bad, so you will need to add a swithc inside your callback.

    function add_header_info() {
      if (!is_page('page-slug')) return;
      global $wpdb, $FileContent;
      // ...
    

Comments are closed.