blob: 8d2f06de7f2c43b808f51fcf23de20290600bdf5 [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>
admin10c3f412006-10-08 07:21:12 +000036<td><h1>Code Igniter User Guide Version 1.5.0b1</h1></td>
admin939906b2006-10-11 19:39:48 +000037<td id="breadcrumb_right"><a href="../toc.html">Linear Table of Contents</a></td>
admina59c2a12006-10-01 19:02:11 +000038</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>
adminb1fddc02006-10-09 21:29:07 +000074
admin70d553b2006-10-01 19:20:24 +000075
admine95014f2006-10-01 21:44:52 +000076<h2>Usage Example</h2>
admin70d553b2006-10-01 19:20:24 +000077
admine95014f2006-10-01 21:44:52 +000078<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
admin78ce3cc2006-10-02 02:58:03 +000093<h1>Function Reference</h1>
admine95014f2006-10-01 21:44:52 +000094
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
98given to the file, the second parameter must contain the file data:</p>
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
107
108<p>If you would like to compress a file that exists somewhere on your server you will need to read the data before
109passing it to the function:</p>
110
111<code>
112$name = 'photo.jpg';<br /><br />
113$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents<br />
114<br />
115$this->zip->add_data($name, $data);
116</code>
117
118
119<p>You are allowed to have multiple calls to this function in order to
120add several files to your archive. Example:</p>
admin70d553b2006-10-01 19:20:24 +0000121
122<code>
123$name = 'mydata1.txt';<br />
124$data = 'A Data String!';<br />
125$this->zip->add_data($name, $data);<br />
126<br />
127$name = 'mydata2.txt';<br />
128$data = 'Another Data String!';<br />
129$this->zip->add_data($name, $data);<br />
admin70d553b2006-10-01 19:20:24 +0000130</code>
131
admine95014f2006-10-01 21:44:52 +0000132<p>Or you can pass multiple files using an array:</p>
admin70d553b2006-10-01 19:20:24 +0000133
admin73fbde22006-10-01 19:26:15 +0000134<code>
135$data = array(<br />
admine95014f2006-10-01 21:44:52 +0000136&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mydata1.txt' => 'A Data String!',<br />
137&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'mydata2.txt' => 'Another Data String!',<br />
138&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 +0000139&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br />
140<br />
141$this->zip->add_data($data);<br />
142<br />
admine95014f2006-10-01 21:44:52 +0000143$this->zip->download('my_backup.zip');
admin73fbde22006-10-01 19:26:15 +0000144</code>
admina59c2a12006-10-01 19:02:11 +0000145
admine95014f2006-10-01 21:44:52 +0000146<p>If you would like your compressed data organized into sub-folders, include the path as part of the filename:</p>
147
148<code>
149$name = '<kbd>personal/</kbd>my_bio.txt';<br />
150$data = 'I was born in an elevator...';<br />
151<br />
152$this->zip->add_data($name, $data);
153</code>
154
155<p>The above example will place <dfn>my_bio.txt</dfn> inside a folder called <kbd>personal</kbd>.</p>
156
157
158<h2>$this->zip->add_dir()</h2>
159
160<p>Permits you to add a directory. Usually this function is unnecessary since you can place your data into folders when
161using <dfn>$this->zip->add_data()</dfn>, but if you would like to create an empty folder you can do so. Example:</p>
162
163<code>$this->zip->add_dir('myfolder'); // Creates a folder called "myfolder"</code>
164
165
166<h2>$this->zip->archive()</h2>
167
168<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
169directory is writable (666 or 777 is usually OK). Example:</p>
170
171<code>$this->zip->archive('/path/to/folder/myarchive.zip'); // Creates a file named myarchive.zip</code>
172
173
174<h2>$this->zip->download()</h2>
175
176<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.
177Example:</p>
178
179<code>$this->zip->download('latest_stuff.zip'); // File will be named "latest_stuff.zip"</code>
180
181<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
182that cause the download to happen and the file to be treated as binary.</p>
183
184
185<h2>$this->zip->get_zip()</h2>
186
187<p>Returns the Zip-compressed file data. Generally you will not need this function unless you want to do something unique with the data.
188Example:</p>
189
190<code>
191$name = 'my_bio.txt';<br />
192$data = 'I was born in an elevator...';<br />
193<br />
194$this->zip->add_data($name, $data);<br /><br />
195
196$zip_file = $this->zip->get_zip();
197</code>
198
199
200<h2>$this->zip->clear_data()</h2>
201
202<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.
203If, however, you need to create multiple Zips, each with different data, you can clear the cache between calls. Example:</p>
204
205<code>
206$name = 'my_bio.txt';<br />
207$data = 'I was born in an elevator...';<br />
208<br />
209$this->zip->add_data($name, $data);<br />
210$zip_file = $this->zip->get_zip();<br />
211<br />
212<kbd>$this->zip->clear_data();</kbd>
213<br /><br />
214
215$name = 'photo.jpg';<br />
216$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents<br />
217$this->zip->add_data($name, $data);
218<br /><br />
219$this->zip->download('myphotos.zip');
220</code>
221
222
223
224
225
226
227
228
229
admina59c2a12006-10-01 19:02:11 +0000230
231
232
233
234</div>
235<!-- END CONTENT -->
236
237
238<div id="footer">
239<p>
240Previous Topic:&nbsp;&nbsp;<a href="validation.html">Validation Class</a>
241&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
242<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
243<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
244Next Topic:&nbsp;&nbsp;<a href="../helpers/array_helper.html">Array Helper</a>
245<p>
246<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>
247</div>
248
249</body>
250</html>