download/print file recorded in postgresql database with php

Asked

Viewed 147 times

0

I uploaded a file to the postgresql database with the OID column type. With the code below I can view/open the file in the browser, but I intend to download and/or print that same file on the printer with php. The files will be of any extension.

$query= pg_query($dbconn,"select * from tabela") or die(pg_last_error($dbconn));
$queryRow= pg_fetch_assoc($query);
$file = $queryRow['doc'];

pg_query($dbconn, "begin");
$handle = pg_lo_open($dbconn, $file, "r");
pg_lo_read_all($handle);
pg_query($dbconn, "commit");

1 answer

0


Resolved as follows:

// consulta postgresql
$query = pg_query($dbconn,"select *, encode(lo_get(arquivo),'base64') as arquivo1 from tabela where id = 1") or die(pg_last_error($dbconn));
$queryRow = pg_fetch_assoc($query);

// link para download
<a download="arquivo.png" href="data:image/png;base64,<?php echo $queryRow['arquivo1'];?>">Download</a>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.