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