Using PHP insert a PDF into a Postgres table, then display PDF to website

Job ID: 33835905

Budget: $30 – $250 NZD

Using PHP I'm trying to store a PDF into a Postgres table; to then be able to read / display the PDF contents from the website.

Currently I have the below python code which inserts the PDF into the Postgres table.
The PDF is displayed when called from the website i.e. https://website/documentname.pdf

The below PHP code stores the PDF, but when it comes time to read/display it to the website... it times out.
Clearly its not storing it quite the same as the Python code.

The Postgres database sits on a remote server. I've probably looked at this too long and i'm using one too many bytea or escape functions

OS is Ubuntu 20.04

PHP
$foPDF = fopen($fsFilepath, 'rb');
$foPDFContent = fread($foPDF, filesize($fsFilepath));
$foPDFContent = file_get_contents($fsFilepath);
$foPDFContent = pg_escape_bytea($foPDFContent);
$foPDFContent = pg_escape_string($foPDFContent);
fclose($foPDF);

INSERT INTO table(
date,
name,
mime,
size,
file,
title,
pages)
VALUES (
'{$psPdfDate}',
'{$psDocumentName}',
'application/pdf',
{$foPDFInfo['File size']},
'{$foPDFContent}'::bytea,
'{$psPdfTitle}',
{$foPDFInfo['Pages']})
ON CONFLICT (name, date)
DO UPDATE SET file = EXCLUDED.file,
size = EXCLUDED.size,
title = EXCLUDED.title,
pages = EXCLUDED.pages;


PYTHON
pdfFileObj = open(gsFilepath + fsFilename, 'rb')
pdfMem = memoryview(pdfFileObj.read())
foPDF = db.Binary(pdfMem)

foRS.execute("""UPDATE documents
SET file = %s,
size = %i,
title = '%s',
pages = %i,
tags = '{%s}'
WHERE name = '%s'
AND date = '%s';""" %
(foPDF,
.
.
Related categories: PHP PostgreSQL Ubuntu