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 |
| 24 | :param string $data: File contents |
| 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 | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame^] | 33 | If 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 |
| 35 | handler for that type - it can use it. |
| 36 | |
| 37 | Example:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
| 39 | $data = 'Here is some text!'; |
| 40 | $name = 'mytext.txt'; |
| 41 | force_download($name, $data); |
| 42 | |
| 43 | If you want to download an existing file from your server you'll need to |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame^] | 44 | read the file into a string:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 45 | |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame^] | 46 | $data = file_get_contents('/path/to/photo.jpg'); // Read the file's contents |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 47 | $name = 'myphoto.jpg'; |
Andrey Andreev | 48a8675 | 2012-11-08 15:16:34 +0200 | [diff] [blame^] | 48 | force_download($name, $data); |