blob: f126bd0b7cb443d93098a016cdce525c012fa1a9 [file] [log] [blame]
admina59c2a12006-10-01 19:02:11 +00001<!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">
14window.onload = function() {
15 myHeight = new fx.Height('nav', {duration: 400});
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>
36<td><h1>Code Igniter User Guide Version 1.5.0</h1></td>
37<td id="breadcrumb_right"><a href="../toc.html">Full Table of Contents</a></td>
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> &nbsp;&#8250;&nbsp;
49<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
50Zip 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&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<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>
admine95014f2006-10-01 21:44:52 +000065<p>Code Igniter's Zip Encoding Class classes permit you to create Zip archives. Archives can be downloaded to your
66desktop or saved to a directory.</p>
admina59c2a12006-10-01 19:02:11 +000067
68
admin70d553b2006-10-01 19:20:24 +000069<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>
admine95014f2006-10-01 21:44:52 +000073<p>Once loaded, the Zip library object will be available using: <dfn>$this->zip</dfn></p>
admin70d553b2006-10-01 19:20:24 +000074
admine95014f2006-10-01 21:44:52 +000075<h2>Usage Example</h2>
admin70d553b2006-10-01 19:20:24 +000076
admine95014f2006-10-01 21:44:52 +000077<p>This example demonstrates how to compress a file, save it to a folder on your server, and download it to your desktop.</p>
78
79<code>
80$name = 'mydata1.txt';<br />
81$data = 'A Data String!';<br />
82<br />
83$this->zip->add_data($name, $data);<br />
84<br />
85// Write the zip file to a folder on your server. Name it "my_backup.zip"<br />
86$this->zip->archive('/path/to/directory/my_backup.zip');
87<br /><br />
88 // Download the file to your desktop. Name it "my_backup.zip"<br />
89$this->zip->download('my_backup.zip');
90</code>
91
92<h1><br />Function Reference</h1>
93
94<h2>$this->zip->add_data()</h2>
95
96<p>Permits you to add data to the Zip archive. The first parameter must contain the name you would like
97given to the file, the second parameter must contain the file data:</p>
98
99<code>
100$name = 'my_bio.txt';<br />
101$data = 'I was born in an elevator...';<br />
102<br />
103$this->zip->add_data($name, $data);
104</code>
105
106
107<p>If you would like to compress a file that exists somewhere on your server you will need to read the data before
108passing it to the function:</p>
109
110<code>
111$name = 'photo.jpg';<br /><br />
112$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents<br />
113<br />
114$this->zip->add_data($name, $data);
115</code>
116
117
118<p>You are allowed to have multiple calls to this function in order to
119add several files to your archive. Example:</p>
admin70d553b2006-10-01 19:20:24 +0000120
121<code>
122$name = 'mydata1.txt';<br />
123$data = 'A Data String!';<br />
124$this->zip->add_data($name, $data);<br />
125<br />
126$name = 'mydata2.txt';<br />
127$data = 'Another Data String!';<br />
128$this->zip->add_data($name, $data);<br />
admin70d553b2006-10-01 19:20:24 +0000129</code>
130
admine95014f2006-10-01 21:44:52 +0000131<p>Or you can pass multiple files using an array:</p>
admin70d553b2006-10-01 19:20:24 +0000132
admin73fbde22006-10-01 19:26:15 +0000133<code>
134$data = array(<br />
admine95014f2006-10-01 21:44:52 +0000135&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mydata1.txt' => 'A Data String!',<br />
136&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mydata2.txt' => 'Another Data String!',<br />
137&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'photo.jpg' => file_get_contents("/path/to/photo.jpg")<br />
admin73fbde22006-10-01 19:26:15 +0000138&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
139<br />
140$this->zip->add_data($data);<br />
141<br />
admine95014f2006-10-01 21:44:52 +0000142$this->zip->download('my_backup.zip');
admin73fbde22006-10-01 19:26:15 +0000143</code>
admina59c2a12006-10-01 19:02:11 +0000144
admine95014f2006-10-01 21:44:52 +0000145<p>If you would like your compressed data organized into sub-folders, include the path as part of the filename:</p>
146
147<code>
148$name = '<kbd>personal/</kbd>my_bio.txt';<br />
149$data = 'I was born in an elevator...';<br />
150<br />
151$this->zip->add_data($name, $data);
152</code>
153
154<p>The above example will place <dfn>my_bio.txt</dfn> inside a folder called <kbd>personal</kbd>.</p>
155
156
157<h2>$this->zip->add_dir()</h2>
158
159<p>Permits you to add a directory. Usually this function is unnecessary since you can place your data into folders when
160using <dfn>$this->zip->add_data()</dfn>, but if you would like to create an empty folder you can do so. Example:</p>
161
162<code>$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"</code>
163
164
165<h2>$this->zip->archive()</h2>
166
167<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
168directory is writable (666 or 777 is usually OK). Example:</p>
169
170<code>$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip</code>
171
172
173<h2>$this->zip->download()</h2>
174
175<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.
176Example:</p>
177
178<code>$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"</code>
179
180<p class="important"><strong>Note:</strong>&nbsp; Do not display any data in the controller in which you call this function since it sends various server headers
181that cause the download to happen and the file to be treated as binary.</p>
182
183
184<h2>$this->zip->get_zip()</h2>
185
186<p>Returns the Zip-compressed file data. Generally you will not need this function unless you want to do something unique with the data.
187Example:</p>
188
189<code>
190$name = 'my_bio.txt';<br />
191$data = 'I was born in an elevator...';<br />
192<br />
193$this->zip->add_data($name, $data);<br /><br />
194
195$zip_file = $this->zip->get_zip();
196</code>
197
198
199<h2>$this->zip->clear_data()</h2>
200
201<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.
202If, however, you need to create multiple Zips, each with different data, you can clear the cache between calls. Example:</p>
203
204<code>
205$name = 'my_bio.txt';<br />
206$data = 'I was born in an elevator...';<br />
207<br />
208$this->zip->add_data($name, $data);<br />
209$zip_file = $this->zip->get_zip();<br />
210<br />
211<kbd>$this->zip->clear_data();</kbd>
212<br /><br />
213
214$name = 'photo.jpg';<br />
215$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents<br />
216$this->zip->add_data($name, $data);
217<br /><br />
218$this->zip->download('myphotos.zip');
219</code>
220
221
222
223
224
225
226
227
228
admina59c2a12006-10-01 19:02:11 +0000229
230
231
232
233</div>
234<!-- END CONTENT -->
235
236
237<div id="footer">
238<p>
239Previous Topic:&nbsp;&nbsp;<a href="validation.html">Validation Class</a>
240&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
241<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
242<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
243Next Topic:&nbsp;&nbsp;<a href="../helpers/array_helper.html">Array Helper</a>
244<p>
245<p><a href="http://www.codeigniter.com">Code Igniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006 &nbsp;&middot;&nbsp; <a href="http://www.pmachine.com">pMachine, Inc.</a></p>
246</div>
247
248</body>
249</html>