<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blogternals &#187; read</title>
	<atom:link href="http://www.blogternals.com/tag/read/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.blogternals.com</link>
	<description>Knowledge Belongs to the World!</description>
	<lastBuildDate>Wed, 11 Aug 2010 12:32:12 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>PHP &#8211; Manipulating Files</title>
		<link>http://www.blogternals.com/2009/06/16/php-manipulating-files/</link>
		<comments>http://www.blogternals.com/2009/06/16/php-manipulating-files/#comments</comments>
		<pubDate>Tue, 16 Jun 2009 16:54:53 +0000</pubDate>
		<dc:creator>Allen Sanford</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[files]]></category>
		<category><![CDATA[read]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[write]]></category>

		<guid isPermaLink="false">http://www.codeternals.com/?p=694</guid>
		<description><![CDATA[Now I know there are a hundred and one tutorials on how to manipulate files with PHP, but I have to write this tutorial for no other reason than to practice writing. You see I aspire to perhaps write a book or two about programming on the web and well, I am going to need [...]]]></description>
			<content:encoded><![CDATA[<p>Now I know there are a hundred and one tutorials on how to manipulate files with PHP, but I have to write this tutorial for no other reason than to practice writing. You see I aspire to perhaps write a book or two about programming on the web and well, I am going to need the practice before I actually write the books. So here we go. For those of you who don&#8217;t know already my writings are of the southern variety, and I usually don&#8217;t try to sound all refined and sophisticated, I usually leave out the unneeded technical mumbo jumbo and stick with just the useful stuff, and sometimes things come out of no where in my writings.</p>
<p><span id="more-694"></span></p>
<p>If you plan on doing any kinda of programming for the web, and you do it long enough you are going to run across the need to manipulate files. The creators of PHP were kind enough to give us all kinds of file manipulation routines, some are even a security risk so be careful. Not only are some of these function a security risk but you can tear things up in the system core files or configuration files, for simplicity lets just say you can do a lot of damage, OK. Alright so here is my disclaimer speech, one you mess it up it an&#8217;t my fault, two make sure apache is using the www-data user this will help minimize any damage should things go wrong, and three backups are your friend, ok enough onward we go.</p>
<p>Well I guess first things first, if you want to be able to manipulate files I guess you have to have a file to manipulate. Enter <em>fopen</em> now the <em>fopen</em> function needs two parameters, one the filename and two the mode in which to operate which can be anyone of the following:</p>
<table border="0">
<thead>
<tr valign="middle">
<th><em><tt>mode</tt></em> </th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr valign="middle">
<td align="left"><em>&#8216;r&#8217;</em></td>
<td align="left">Open for reading only; place the file pointer at the             beginning of the file.</td>
</tr>
<tr valign="middle">
<td align="left"><em>&#8216;r+&#8217;</em></td>
<td align="left">Open for reading and writing; place the file pointer at             the beginning of the file.</td>
</tr>
<tr valign="middle">
<td align="left"><em>&#8216;w&#8217;</em></td>
<td align="left">Open for writing only; place the file pointer at the             beginning of the file and truncate the file to zero length.             If the file does not exist, attempt to create it.</td>
</tr>
<tr valign="middle">
<td align="left"><em>&#8216;w+&#8217;</em></td>
<td align="left">Open for reading and writing; place the file pointer at             the beginning of the file and truncate the file to zero             length.  If the file does not exist, attempt to create it.</td>
</tr>
<tr valign="middle">
<td align="left"><em>&#8216;a&#8217;</em></td>
<td align="left">Open for writing only; place the file pointer at the end of             the file. If the file does not exist, attempt to create it.</td>
</tr>
<tr valign="middle">
<td align="left"><em>&#8216;a+&#8217;</em></td>
<td align="left">Open for reading and writing; place the file pointer at             the end of the file. If the file does not exist, attempt to             create it.</td>
</tr>
<tr valign="middle">
<td align="left"><em>&#8216;x&#8217;</em></td>
<td align="left">Create and open for writing only; place the file pointer at the             beginning of the file.  If the file already exists, the             <strong>fopen()</strong> call will fail by returning <strong><tt>FALSE</tt></strong> and             generating an error of level <strong><tt>E_WARNING</tt></strong>.  If             the file does not exist, attempt to create it.  This is equivalent             to specifying <em>O_EXCL|O_CREAT</em> flags for the             underlying <em>open(2)</em> system call.</td>
</tr>
<tr valign="middle">
<td align="left"><em>&#8216;x+&#8217;</em></td>
<td align="left">Create and open for reading and writing; place the file pointer at             the beginning of the file.  If the file already exists, the             <strong>fopen()</strong> call will fail by returning <strong><tt>FALSE</tt></strong> and             generating an error of level <strong><tt>E_WARNING</tt></strong>.  If             the file does not exist, attempt to create it.  This is equivalent             to specifying <em>O_EXCL|O_CREAT</em> flags for the             underlying <em>open(2)</em> system call.</td>
</tr>
</tbody>
</table>
<p>Now here is an example:</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.blogternals.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=694&amp;download=example.php">example.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6946"><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code" id="p694code6"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testFile.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Danger, failed to open file!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Kinda easy uh?</p>
<p>So now we have an empty file so I think we need to put some text in it, I opt for Lorem Ipsum text. Enter <i>fwrite</i> function stage left;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.blogternals.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=694&amp;download=example.php">example.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6947"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p694code7"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testFile.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Danger, failed to open file!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac orci metus.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Man I don&#8217;t anyone can understand this stuff it is so complicated <img src='http://www.blogternals.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , those PHP guys were nice to us when they named this stuff. There is also a function called file_put_contents that does what the above code does worth reading up on in the documentation.</p>
<p>OK, so now we can open a file, we can write to a file, how about reading a file. Serious note here there are many ways to read a file in PHP depending on what it is you want to do with it there is fgets, fgetss, fgetc, fgetcsv, file_get_contents so read up in the documentation after you finish up here.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.blogternals.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=694&amp;download=example.php">example.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6948"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p694code8"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testFile.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$handle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'w'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Danger, failed to open file!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$oneLine</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$oneLine</span><span style="color: #339933;">;</span>
<span style="color: #990000;">fclose</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$handle</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The above you guessed it reads one line from the file. Implement a loop here and you could read the whole file.</p>
<p>Now comes the most unintuitive part of this tutorial, deleting a file.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.blogternals.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=694&amp;download=example.php">example.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p6949"><td class="line_numbers"><pre>1
2
</pre></td><td class="code" id="p694code9"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testFile.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Who would have thought unlink? Just to clarify before you can delete it it the handle must be closed.</p>
<p>One last example before you go I mentioned file_gets_contents and file_puts_contents do here is an example of that. Notice the lack of fopen and fclose here.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left2">Download <a href="http://www.blogternals.com/wp-content/plugins/wp-codebox/wp-codebox.php?p=694&amp;download=example.php">example.php</a></span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p69410"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p694code10"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;testFile.txt&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$string</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec ac orci metus.&quot;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">file_put_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$contents</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$contents</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Will echo the entire contents of that file.</span></pre></td></tr></table></div>

<p>Well, there you have it my stab at a file manipulation tutorial in PHP. Sure hope my writing gets better with time. Have a Good&#8217;n! </p>
]]></content:encoded>
			<wfw:commentRss>http://www.blogternals.com/2009/06/16/php-manipulating-files/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
