/* BeejBløg */

Oct 24, 2016 - 1 minute read - Comments -

PHP SQL Server blob hosting

from here

see this post for sql server driver downloads

 

downloadPDF.php

<?php
header('Content-type: application/pdf');
// leave this out to open directly in browser: header('Content-Disposition: attachment; filename="my.pdf"');
include("SQLConnect.php");
$sql = "select InvoiceDocument from SalesInvoicePDF where InvoiceID = '123'";
$stmt = sqlsrv_query($conn, $sql);
if ( sqlsrv_fetch($stmt) )
{
    //this pulls the first field via "0"
    $data = sqlsrv_get_field($stmt, 0, SQLSRV_PHPTYPE_STREAM(SQLSRV_ENC_BINARY));
    // write binary sql stream directly to http response
    fpassthru($data);
}
?>

SQLConnect.php

<?php
// DB connection info
$host = "servername";
$user = "user";
$pwd = "pwd";
$db = "database";
// Connect to database.
try {
  /*
  $connPdo = new PDO( "sqlsrv:Server= $host ; Database = $db ", $user, $pwd);
  $connPdo->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
    */

  $conn = sqlsrv_connect($host, array("Database"=>$db, "UID"=>$user, "PWD"=>$pwd));
}
catch(Exception $e){
   die(var_dump($e));
}
?>