prepare($sql); $sth->execute([$docid]); $row = $sth->fetch(); $dtmap = array( 'generic' => 'doc', 'manual' => 'man', 'invoice' => 'inv', 'picture' => 'pic', 'drawing' => 'drw'); $file = sprintf("%s/%s-%s.%s", $g_doc_basepath, $dtmap[$row['doctype']], $row['hash'], $row['extension']); if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: ' . $row['mimetype']); header('Content-Disposition: attachment; filename="' . $row['filename'] . '"'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); if ($row['docsize'] == 0) { header('Content-Length: ' . filesize($file)); } else { header('Content-Length: ' . $row['docsize']); } readfile($file); exit; } else { // dynamically create error image // TODO }