/** * download files * * outputs file content and set corresponding header params * * @param int $id file id * @return void */ public static function download($id, $versionId = null, $asAttachment = true, $forUseId = false) { $r = empty($versionId) ? DM\Files::read($id) : DM\FilesVersions::read($versionId); if (!empty($r)) { $content = DM\FilesContent::read($r['content_id']); //check if can download file if (!Security::canDownload($r['id'], $forUseId)) { throw new \Exception(L\get('Access_denied')); } header('Content-Description: File Transfer'); header('Content-Type: ' . $content['type'] . '; charset=UTF-8'); if ($asAttachment || $content['type'] !== 'application/pdf') { //purify filename for cases when we have a wrong filename in the system already header('Content-Disposition: attachment; filename="' . Purify::filename($r['name']) . '"'); } header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . $content['size']); readfile(Config::get('files_dir') . $content['path'] . DIRECTORY_SEPARATOR . $content['id']); } else { throw new \Exception(L\get('Object_not_found')); } }
/** * download files * * outputs file content and set corresponding header params * * @param int $id file id * @return void */ public static function download($id, $versionId = null, $asAttachment = true, $forUseId = false) { $sql = empty($versionId) ? 'SELECT f.id ,f.content_id ,c.path ,f.name ,c.`type` ,c.size FROM files f LEFT JOIN files_content c ON f.content_id = c.id WHERE f.id = $1' : 'SELECT f.file_id `id` ,f.id `version_id` ,f.content_id ,c.path ,f.name ,c.`type` ,c.size FROM files_versions f LEFT JOIN files_content c ON f.content_id = c.id WHERE f.id = $1'; $res = DB\dbQuery($sql, Util\coalesce($versionId, $id)) or die(DB\dbQueryError()); if ($r = $res->fetch_assoc()) { //check if can download file if (!Security::canDownload($r['id'], $forUseId)) { throw new \Exception(L\get('Access_denied')); } header('Content-Description: File Transfer'); header('Content-Type: ' . $r['type'] . '; charset=UTF-8'); if ($asAttachment || $r['type'] !== 'application/pdf') { //purify filename for cases when we have a wrong filename in the system already header('Content-Disposition: attachment; filename="' . Purify::filename($r['name']) . '"'); } header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate'); header('Pragma: public'); header('Content-Length: ' . $r['size']); readfile(Config::get('files_dir') . $r['path'] . DIRECTORY_SEPARATOR . $r['content_id']); } else { throw new \Exception(L\get('Object_not_found')); } $res->close(); }