-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDownloadAction.php
68 lines (64 loc) · 2.29 KB
/
DownloadAction.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
/**
* User: Scott_Huang
* Date: 6/16/2015
* Time: 5:48 PM
*/
namespace scotthuangzl\export2excel;
use Yii;
use yii\base\Action;
class DownloadAction extends Action
{
public function run($file_name, $file_type = 'excel', $deleteAfterDownload = false) {
if (empty($file_name)) {
// return $this->goBack();
return 0;
}
$baseRoot = Yii::getAlias('@webroot') . "/uploads/";
$file_name = $baseRoot . $file_name;
//echo $file_name,"<BR/>";
if (!file_exists($file_name)) {
//HzlUtil::setMsg("Error", "File not exist");
return 0;
}
$fp = fopen($file_name, "r");
$file_size = filesize($file_name);
//下载文件需要用到的头
if ($file_type == 'excel') {
header('Pragma: public');
header('Expires: 0');
header('Content-Encoding: none');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: public');
header('Content-Type: application/vnd.ms-excel');
header('Content-Description: File Transfer');
Header("Content-Disposition: attachment; filename=" . basename($file_name));
header('Content-Transfer-Encoding: binary');
Header("Content-Length:" . $file_size);
} else if ($file_type == 'picture') { //pictures
Header("Content-Type:image/jpeg");
Header("Accept-Ranges: bytes");
Header("Content-Disposition: attachment; filename=" . basename($file_name));
Header("Content-Length:" . $file_size);
} else { //other files
Header("Content-type: application/octet-stream");
Header("Accept-Ranges: bytes");
Header("Content-Disposition: attachment; filename=" . basename($file_name));
Header("Content-Length:" . $file_size);
}
$buffer = 1024;
$file_count = 0;
//向浏览器返回数据
while (!feof($fp) && $file_count < $file_size) {
$file_con = fread($fp, $buffer);
$file_count+=$buffer;
echo $file_con;
}
//echo fread($fp, $file_size);
fclose($fp);
if ($deleteAfterDownload) {
unlink($file_name);
}
return 1;
}
}