admin | a59c2a1 | 2006-10-01 19:02:11 +0000 | [diff] [blame] | 1 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
| 2 | <html>
|
| 3 | <head>
|
| 4 |
|
| 5 | <title>Code Igniter User Guide</title>
|
| 6 |
|
| 7 | <style type='text/css' media='all'>@import url('../userguide.css');</style>
|
| 8 | <link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
|
| 9 |
|
| 10 | <script type="text/javascript" src="../nav/nav.js"></script>
|
| 11 | <script type="text/javascript" src="../nav/prototype.lite.js"></script>
|
| 12 | <script type="text/javascript" src="../nav/moo.fx.js"></script>
|
| 13 | <script type="text/javascript">
|
| 14 | window.onload = function() {
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 15 | myHeight = new fx.Height('nav', {duration: 400});
|
admin | a59c2a1 | 2006-10-01 19:02:11 +0000 | [diff] [blame] | 16 | myHeight.hide();
|
| 17 | }
|
| 18 | </script>
|
| 19 |
|
| 20 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
| 21 | <meta http-equiv='expires' content='-1' />
|
| 22 | <meta http-equiv= 'pragma' content='no-cache' />
|
| 23 | <meta name='robots' content='all' />
|
| 24 | <meta name='author' content='Rick Ellis' />
|
| 25 | <meta name='description' content='Code Igniter User Guide' />
|
| 26 |
|
| 27 | </head>
|
| 28 | <body>
|
| 29 |
|
| 30 | <!-- START NAVIGATION -->
|
| 31 | <div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
|
| 32 | <div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
|
| 33 | <div id="masthead">
|
| 34 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 35 | <tr>
|
admin | e0cd609 | 2006-10-20 01:00:31 +0000 | [diff] [blame] | 36 | <td><h1>Code Igniter User Guide Version 1.5.0b3</h1></td>
|
admin | 939906b | 2006-10-11 19:39:48 +0000 | [diff] [blame] | 37 | <td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
|
admin | a59c2a1 | 2006-10-01 19:02:11 +0000 | [diff] [blame] | 38 | </tr>
|
| 39 | </table>
|
| 40 | </div>
|
| 41 | <!-- END NAVIGATION -->
|
| 42 |
|
| 43 |
|
| 44 | <!-- START BREADCRUMB -->
|
| 45 | <table cellpadding="0" cellspacing="0" border="0" style="width:100%">
|
| 46 | <tr>
|
| 47 | <td id="breadcrumb">
|
| 48 | <a href="http://www.codeigniter.com/">Code Igniter Home</a> ›
|
| 49 | <a href="../index.html">User Guide Home</a> ›
|
| 50 | Zip Encoding Class
|
| 51 | </td>
|
| 52 | <td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="www.codeigniter.com/user_guide/" />Search User Guide <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" /> <input type="submit" class="submit" name="sa" value="Go" /></form></td>
|
| 53 | </tr>
|
| 54 | </table>
|
| 55 | <!-- END BREADCRUMB -->
|
| 56 |
|
| 57 | <br clear="all" />
|
| 58 |
|
| 59 |
|
| 60 | <!-- START CONTENT -->
|
| 61 | <div id="content">
|
| 62 |
|
| 63 |
|
| 64 | <h1>Zip Encoding Class</h1>
|
admin | e334c47 | 2006-10-21 19:44:22 +0000 | [diff] [blame] | 65 | <p>Code Igniter's Zip Encoding Class classes permit you to create Zip archives. Archives can be downloaded to your
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 66 | desktop or saved to a directory.</p>
|
admin | a59c2a1 | 2006-10-01 19:02:11 +0000 | [diff] [blame] | 67 |
|
| 68 |
|
admin | 70d553b | 2006-10-01 19:20:24 +0000 | [diff] [blame] | 69 | <h2>Initializing the Class</h2>
|
| 70 | <p>Like most other classes in Code Igniter, the Zip class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
|
| 71 |
|
| 72 | <code>$this->load->library('zip');</code>
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 73 | <p>Once loaded, the Zip library object will be available using: <dfn>$this->zip</dfn></p>
|
admin | b1fddc0 | 2006-10-09 21:29:07 +0000 | [diff] [blame] | 74 |
|
admin | 70d553b | 2006-10-01 19:20:24 +0000 | [diff] [blame] | 75 |
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 76 | <h2>Usage Example</h2>
|
admin | 70d553b | 2006-10-01 19:20:24 +0000 | [diff] [blame] | 77 |
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 78 | <p>This example demonstrates how to compress a file, save it to a folder on your server, and download it to your desktop.</p>
|
| 79 |
|
| 80 | <code>
|
| 81 | $name = 'mydata1.txt';<br />
|
| 82 | $data = 'A Data String!';<br />
|
| 83 | <br />
|
| 84 | $this->zip->add_data($name, $data);<br />
|
| 85 | <br />
|
| 86 | // Write the zip file to a folder on your server. Name it "my_backup.zip"<br />
|
| 87 | $this->zip->archive('/path/to/directory/my_backup.zip');
|
| 88 | <br /><br />
|
| 89 | // Download the file to your desktop. Name it "my_backup.zip"<br />
|
| 90 | $this->zip->download('my_backup.zip');
|
| 91 | </code>
|
| 92 |
|
admin | 78ce3cc | 2006-10-02 02:58:03 +0000 | [diff] [blame] | 93 | <h1>Function Reference</h1>
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 94 |
|
| 95 | <h2>$this->zip->add_data()</h2>
|
| 96 |
|
| 97 | <p>Permits you to add data to the Zip archive. The first parameter must contain the name you would like
|
admin | 9f8243b | 2006-10-23 20:00:15 +0000 | [diff] [blame^] | 98 | given to the file, the second parameter must contain the file data as a string:</p>
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 99 |
|
| 100 | <code>
|
| 101 | $name = 'my_bio.txt';<br />
|
| 102 | $data = 'I was born in an elevator...';<br />
|
| 103 | <br />
|
| 104 | $this->zip->add_data($name, $data);
|
| 105 | </code>
|
| 106 |
|
admin | 9f8243b | 2006-10-23 20:00:15 +0000 | [diff] [blame^] | 107 | <p>You are allowed multiple calls to this function in order to
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 108 | add several files to your archive. Example:</p>
|
admin | 70d553b | 2006-10-01 19:20:24 +0000 | [diff] [blame] | 109 |
|
| 110 | <code>
|
| 111 | $name = 'mydata1.txt';<br />
|
| 112 | $data = 'A Data String!';<br />
|
| 113 | $this->zip->add_data($name, $data);<br />
|
| 114 | <br />
|
| 115 | $name = 'mydata2.txt';<br />
|
| 116 | $data = 'Another Data String!';<br />
|
| 117 | $this->zip->add_data($name, $data);<br />
|
admin | 70d553b | 2006-10-01 19:20:24 +0000 | [diff] [blame] | 118 | </code>
|
| 119 |
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 120 | <p>Or you can pass multiple files using an array:</p>
|
admin | 70d553b | 2006-10-01 19:20:24 +0000 | [diff] [blame] | 121 |
|
admin | 73fbde2 | 2006-10-01 19:26:15 +0000 | [diff] [blame] | 122 | <code>
|
| 123 | $data = array(<br />
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 124 | 'mydata1.txt' => 'A Data String!',<br />
|
admin | 9f8243b | 2006-10-23 20:00:15 +0000 | [diff] [blame^] | 125 | 'mydata2.txt' => 'Another Data String!'<br />
|
admin | 73fbde2 | 2006-10-01 19:26:15 +0000 | [diff] [blame] | 126 | );<br />
|
| 127 | <br />
|
| 128 | $this->zip->add_data($data);<br />
|
| 129 | <br />
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 130 | $this->zip->download('my_backup.zip');
|
admin | 73fbde2 | 2006-10-01 19:26:15 +0000 | [diff] [blame] | 131 | </code>
|
admin | a59c2a1 | 2006-10-01 19:02:11 +0000 | [diff] [blame] | 132 |
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 133 | <p>If you would like your compressed data organized into sub-folders, include the path as part of the filename:</p>
|
| 134 |
|
| 135 | <code>
|
| 136 | $name = '<kbd>personal/</kbd>my_bio.txt';<br />
|
| 137 | $data = 'I was born in an elevator...';<br />
|
| 138 | <br />
|
| 139 | $this->zip->add_data($name, $data);
|
| 140 | </code>
|
| 141 |
|
| 142 | <p>The above example will place <dfn>my_bio.txt</dfn> inside a folder called <kbd>personal</kbd>.</p>
|
| 143 |
|
| 144 |
|
| 145 | <h2>$this->zip->add_dir()</h2>
|
| 146 |
|
| 147 | <p>Permits you to add a directory. Usually this function is unnecessary since you can place your data into folders when
|
| 148 | using <dfn>$this->zip->add_data()</dfn>, but if you would like to create an empty folder you can do so. Example:</p>
|
| 149 |
|
| 150 | <code>$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"</code>
|
| 151 |
|
| 152 |
|
admin | 9f8243b | 2006-10-23 20:00:15 +0000 | [diff] [blame^] | 153 |
|
| 154 | <h2>$this->zip->read_file()</h2>
|
| 155 |
|
| 156 | <p>Permits you to compress a file that already exists somewhere on your server. Supply a file path and the zip class will
|
| 157 | read it and add it to the archive:</p>
|
| 158 |
|
| 159 | <code>
|
| 160 | $path = '/path/to/photo.jpg';<br /><br />
|
| 161 | $this->zip->read_file($path);
|
| 162 | <br /><br />
|
| 163 | // Download the file to your desktop. Name it "my_backup.zip"<br />
|
| 164 | $this->zip->download('my_backup.zip');
|
| 165 | </code>
|
| 166 |
|
| 167 | <p>If you would like the Zip archive to maintain the directory structure the file is in, pass <kbd>TRUE</kbd> (boolean) in the
|
| 168 | second parameter. Example:
|
| 169 |
|
| 170 |
|
| 171 | <code>
|
| 172 | $path = '/path/to/photo.jpg';<br /><br />
|
| 173 | $this->zip->read_file($path, <kbd>TRUE</kbd>);
|
| 174 | <br /><br />
|
| 175 | // Download the file to your desktop. Name it "my_backup.zip"<br />
|
| 176 | $this->zip->download('my_backup.zip');
|
| 177 | </code>
|
| 178 |
|
| 179 | <p>In the above example, <dfn>photo.jpg</dfn> will be placed inside two folders: <kbd>path/to/</kbd></p>
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 | <h2>$this->zip->read_dir()</h2>
|
| 184 |
|
| 185 | <p>Permits you to compress a folder (and its contents) that already exists somewhere on your server. Supply a file path to the
|
| 186 | directory and the zip class will recursively read it and recreate it as a Zip archive. All files contained within the
|
| 187 | supplied path will be encodes, as will any sub-folders contained within it. Example:</p>
|
| 188 |
|
| 189 | <code>
|
| 190 | $path = '/path/to/your/directory/';<br /><br />
|
| 191 | $this->zip->read_dir($path);
|
| 192 | <br /><br />
|
| 193 | // Download the file to your desktop. Name it "my_backup.zip"<br />
|
| 194 | $this->zip->download('my_backup.zip');
|
| 195 | </code>
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 200 | <h2>$this->zip->archive()</h2>
|
| 201 |
|
| 202 | <p>Writes the Zip-encoded file to a directory on your server. Submit a valid server path ending in the file name. Make sure the
|
| 203 | directory is writable (666 or 777 is usually OK). Example:</p>
|
| 204 |
|
| 205 | <code>$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip</code>
|
| 206 |
|
| 207 |
|
| 208 | <h2>$this->zip->download()</h2>
|
| 209 |
|
| 210 | <p>Causes the Zip file to be downloaded to your server. The function must be passed the name you would like the zip file called.
|
| 211 | Example:</p>
|
| 212 |
|
| 213 | <code>$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"</code>
|
| 214 |
|
| 215 | <p class="important"><strong>Note:</strong> Do not display any data in the controller in which you call this function since it sends various server headers
|
| 216 | that cause the download to happen and the file to be treated as binary.</p>
|
| 217 |
|
| 218 |
|
| 219 | <h2>$this->zip->get_zip()</h2>
|
| 220 |
|
| 221 | <p>Returns the Zip-compressed file data. Generally you will not need this function unless you want to do something unique with the data.
|
| 222 | Example:</p>
|
| 223 |
|
| 224 | <code>
|
| 225 | $name = 'my_bio.txt';<br />
|
| 226 | $data = 'I was born in an elevator...';<br />
|
| 227 | <br />
|
| 228 | $this->zip->add_data($name, $data);<br /><br />
|
| 229 |
|
| 230 | $zip_file = $this->zip->get_zip();
|
| 231 | </code>
|
| 232 |
|
| 233 |
|
| 234 | <h2>$this->zip->clear_data()</h2>
|
| 235 |
|
| 236 | <p>The Zip class caches your zip data so that it doesn't need to recompile the Zip archive for each function you use above.
|
| 237 | If, however, you need to create multiple Zips, each with different data, you can clear the cache between calls. Example:</p>
|
| 238 |
|
| 239 | <code>
|
| 240 | $name = 'my_bio.txt';<br />
|
| 241 | $data = 'I was born in an elevator...';<br />
|
| 242 | <br />
|
| 243 | $this->zip->add_data($name, $data);<br />
|
| 244 | $zip_file = $this->zip->get_zip();<br />
|
| 245 | <br />
|
| 246 | <kbd>$this->zip->clear_data();</kbd>
|
| 247 | <br /><br />
|
| 248 |
|
| 249 | $name = 'photo.jpg';<br />
|
admin | 9f8243b | 2006-10-23 20:00:15 +0000 | [diff] [blame^] | 250 | $this->zip->read_file("/path/to/photo.jpg"); // Read the file's contents<br />
|
admin | e95014f | 2006-10-01 21:44:52 +0000 | [diff] [blame] | 251 | <br /><br />
|
| 252 | $this->zip->download('myphotos.zip');
|
| 253 | </code>
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
admin | a59c2a1 | 2006-10-01 19:02:11 +0000 | [diff] [blame] | 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 | </div>
|
| 268 | <!-- END CONTENT -->
|
| 269 |
|
| 270 |
|
| 271 | <div id="footer">
|
| 272 | <p>
|
| 273 | Previous Topic: <a href="validation.html">Validation Class</a>
|
| 274 | ·
|
| 275 | <a href="#top">Top of Page</a> ·
|
| 276 | <a href="../index.html">User Guide Home</a> ·
|
| 277 | Next Topic: <a href="../helpers/array_helper.html">Array Helper</a>
|
| 278 | <p>
|
| 279 | <p><a href="http://www.codeigniter.com">Code Igniter</a> · Copyright © 2006 · <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
|
| 280 | </div>
|
| 281 |
|
| 282 | </body>
|
| 283 | </html> |