我想创建一个php页面,用户可以在其中上传pdf文件并将其存储在Postgresql
数据库中。用户可以在另一页上下载或阅读pdf文件。
目前,我发现了这一点:
POSTGRESQL:
CREATE TABLE schema.tab
(
id serial NOT NULL,
a bytea,
CONSTRAINT tab_pkey PRIMARY KEY (id)
)
PHP上传:
if ($_POST[submit]=="submit"){
include_once('../function.php');
ini_set('display_errors','Off');
$db=connection_pgsql() or die('Connessione al DBMS non riuscita');
$data = file_get_contents($_FILES['form_data']['tmp_name']);
$escaped = pg_escape_bytea($data);
$result = pg_prepare( "ins_pic",'INSERT INTO schema.tab (a) VALUES ($1)');
$result = pg_execute ("ins_pic",array('$escaped'));
现在我该如何下载存储在tab中的pdf id=1
?
我试过了:
$sql= "SELECT a FROM schema.tab WHERE id=5";
$resource=pg_query($db, $sql);
$row=pg_fetch_array($resource, NULL, PGSQL_BOTH);
$data = pg_unescape_bytea($row[0]);
$extension ='pdf';
$fileId = 'title';
$filename = $fileId . '.' . $extension;
$fileHandle = fopen($filename, 'w');
fwrite($fileHandle, $data);
fclose($fileHandle);
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
echo $fileHandle;
exit;
但这不起作用!:-(
您必须在php文件中使用标头。输出缓冲区必须为空。$ filecontent代表数据库中的pdf内容。
// We'll be outputting a PDF
header('Content-type: application/pdf');
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
echo $filecontent;
exit;
此代码将确保您的用户将下载pdf文件
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句