<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="bzip2.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.bzip2.html">Bzip2 Functions</a></div> <div class="up"><a href="book.bzip2.html">Bzip2</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="bzip2.examples" class="chapter"> <h1>Examples</h1> <p class="para"> This example opens a temporary file and writes a test string to it, then prints out the contents of the file. </p> <div class="example" id="example-644"> <p><strong>Example #1 Small bzip2 Example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$filename </span><span style="color: #007700">= </span><span style="color: #DD0000">"/tmp/testfile.bz2"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$str </span><span style="color: #007700">= </span><span style="color: #DD0000">"This is a test string.\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// open file for writing<br /></span><span style="color: #0000BB">$bz </span><span style="color: #007700">= </span><span style="color: #0000BB">bzopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"w"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// write string to file<br /></span><span style="color: #0000BB">bzwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$bz</span><span style="color: #007700">, </span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// close file<br /></span><span style="color: #0000BB">bzclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$bz</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// open file for reading<br /></span><span style="color: #0000BB">$bz </span><span style="color: #007700">= </span><span style="color: #0000BB">bzopen</span><span style="color: #007700">(</span><span style="color: #0000BB">$filename</span><span style="color: #007700">, </span><span style="color: #DD0000">"r"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// read 10 characters<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">bzread</span><span style="color: #007700">(</span><span style="color: #0000BB">$bz</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// output until end of the file (or the next 1024 char) and close it. <br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">bzread</span><span style="color: #007700">(</span><span style="color: #0000BB">$bz</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">bzclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$bz</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="bzip2.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.bzip2.html">Bzip2 Functions</a></div> <div class="up"><a href="book.bzip2.html">Bzip2</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>