blob: 1e9ec21ea104a6090d937cfe263da02021cf0664 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001###############
2Download Helper
3###############
4
5The Download Helper lets you download data to your desktop.
6
7.. contents:: Page Contents
8
9Loading this Helper
10===================
11
Andrey Andreev48a86752012-11-08 15:16:34 +020012This helper is loaded using the following code::
Derek Jones8ede1a22011-10-05 13:34:52 -050013
14 $this->load->helper('download');
15
16The following functions are available:
17
Andrey Andreev48a86752012-11-08 15:16:34 +020018force_download()
19================
20
21.. php:function:: force_download($filename = '', $data = '', $set_mime = FALSE)
22
23 :param string $filename: Filename
24 :param string $data: File contents
25 :param bool $set_mime: Whether to try to send the actual MIME type
26 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -050027
28Generates server headers which force data to be downloaded to your
29desktop. Useful with file downloads. The first parameter is the **name
30you want the downloaded file to be named**, the second parameter is the
Andrey Andreev48a86752012-11-08 15:16:34 +020031file data.
Derek Jones8ede1a22011-10-05 13:34:52 -050032
Andrey Andreev48a86752012-11-08 15:16:34 +020033If you set the third parameter to boolean TRUE, then the actual file MIME type
34(based on the filename extension) will be sent, so that if your browser has a
35handler for that type - it can use it.
36
37Example::
Derek Jones8ede1a22011-10-05 13:34:52 -050038
39 $data = 'Here is some text!';
40 $name = 'mytext.txt';
41 force_download($name, $data);
42
43If you want to download an existing file from your server you'll need to
Andrey Andreev48a86752012-11-08 15:16:34 +020044read the file into a string::
Derek Jones8ede1a22011-10-05 13:34:52 -050045
Andrey Andreev48a86752012-11-08 15:16:34 +020046 $data = file_get_contents('/path/to/photo.jpg'); // Read the file's contents
Derek Jones8ede1a22011-10-05 13:34:52 -050047 $name = 'myphoto.jpg';
Andrey Andreev48a86752012-11-08 15:16:34 +020048 force_download($name, $data);