21,886
社区成员
发帖
与我相关
我的任务
分享
create table $module.attach(id serial primary key,name char(200),attachment bytea,type char(200),size integer) with oids
<?php
function bytea_encode($file)
{
$attach_buf = @file_get_contents($file);
return pg_escape_bytea($attach_buf);
}
$name = $_FILES[sheetattachment][name];
$attachment = bytea_encode($_FILES[sheetattachment][tmp_name]);
$type = $_FILES[sheetattachment][type];
$size = $_FILES[sheetattachment][size];
$sql = "insert into attach (name,attachment,type,size)
values ('$name','$attachment'::bytea,'$type',$size)";
$db->query($sql);
?>
<?php
require_once ('global.php');
$id = intval($_GET[id]);
$sql = "select * from attach where id=$id";
$row = $db->getRow($sql);
$attachment = pg_unescape_bytea($row[attachment]);
header('Content-Type: '.$row[type]); //文件类型
header("Content-Length: ".$row[size]); //文件长度
header('Content-Disposition: attachment; filename='.$row[name]); //提示下载
echo $attachment;
exit;
?>