-
Notifications
You must be signed in to change notification settings - Fork 593
/
Action.php
executable file
·68 lines (65 loc) · 2.22 KB
/
Action.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
if (!defined('__TYPECHO_ROOT_DIR__')) exit;
/**
* 文件下载插件,使下载的文件保持上传时的文件名。
*
* @package DownloadFile
* @author DT27
* @version 1.0.0
* @link https://dt27.org/
*/
class DownloadFile_Action extends Typecho_Widget
{
/**
* 构造函数
*
* @param mixed $request
* @param mixed $response
* @param null $params
*/
public function __construct($request, $response, $params = NULL)
{
parent::__construct($request, $response, $params);
}
/**
* 下载文件
*
* @cid 文件ID
* @throws Typecho_Db_Exception
*/
public function downloadFile(){
//throw new Typecho_Widget_Exception(_t('文件不存在'), 404);
//Typecho_Widget::widget('Widget_Contents_Attachment_Edit')->to($attachment);
if($this->request->filter('int')->cid) {
Typecho_Db::get()->fetchRow(Typecho_Db::get()->select()->from('table.contents')
->where('table.contents.type = ?', 'attachment')
->where('table.contents.cid = ?', $this->request->filter('int')->cid)
->limit(1), array($this, 'push'));
if (!$this->have()) {
throw new \InvalidArgumentException("文件不存在");
}
$info = unserialize($this->text);
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false);
header("Content-Type: {$info['mime']}");
header("Content-Disposition: attachment; filename=\"{$info['name']}\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: {$info['size']}");
// download
$file = @fopen($_SERVER['DOCUMENT_ROOT'].$info['path'],"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
}
}