Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############### |
| 2 | Download Helper |
| 3 | ############### |
| 4 | |
| 5 | The Download Helper lets you download data to your desktop. |
| 6 | |
| 7 | .. contents:: Page Contents |
| 8 | |
| 9 | Loading this Helper |
| 10 | =================== |
| 11 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 12 | This helper is loaded using the following code:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 13 | |
| 14 | $this->load->helper('download'); |
| 15 | |
| 16 | The following functions are available: |
| 17 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 18 | force_download() |
| 19 | ================ |
| 20 | |
| 21 | .. php:function:: force_download($filename = '', $data = '', $set_mime = FALSE) |
| 22 | |
| 23 | :param string $filename: Filename |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 24 | :param mixed $data: File contents |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 25 | :param bool $set_mime: Whether to try to send the actual MIME type |
| 26 | :returns: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 27 | |
| 28 | Generates server headers which force data to be downloaded to your |
| 29 | desktop. Useful with file downloads. The first parameter is the **name |
| 30 | you want the downloaded file to be named**, the second parameter is the |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 31 | file data. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 32 | |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 33 | If you set the second parameter to NULL and ``$filename`` is an existing, readable |
| 34 | file path, then its content will be read instead. |
| 35 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame] | 36 | If you set the third parameter to boolean TRUE, then the actual file MIME type |
| 37 | (based on the filename extension) will be sent, so that if your browser has a |
| 38 | handler for that type - it can use it. |
| 39 | |
| 40 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
| 42 | $data = 'Here is some text!'; |
| 43 | $name = 'mytext.txt'; |
| 44 | force_download($name, $data); |
| 45 | |
| 46 | If you want to download an existing file from your server you'll need to |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 47 | do the following:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 48 | |
Andrey Andreev | 53fff91 | 2012-11-22 16:57:23 +0200 | [diff] [blame] | 49 | // Contents of photo.jpg will be automatically read |
| 50 | force_download('/path/to/photo.jpg', NULL); |