<?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>Web Cash &#187; file</title>
	<atom:link href="http://www.earn-web-cash.com/tag/file/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.earn-web-cash.com</link>
	<description>Writing, Designing, and Making Money Online</description>
	<lastBuildDate>Sun, 04 Dec 2011 22:52:34 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Cache Cleanup Script &#8211; Delete Old Files</title>
		<link>http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/</link>
		<comments>http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/#comments</comments>
		<pubDate>Sat, 16 Feb 2008 00:41:00 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[Sundry Musings]]></category>
		<category><![CDATA[cache]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[script]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/</guid>
		<description><![CDATA[The Cache Cleanup Script is designed to do just what it says &#8211; clean up temporary files created in some sort of cache directory. This script is meant to accompany some other kind of script that temporarily stores information in a file. How Cache Cleanup Works Here&#8217;s the source code in text format. At the [...]]]></description>
			<content:encoded><![CDATA[<p>The Cache Cleanup Script is designed to do just what it says &#8211; clean up temporary files created in some sort of cache directory.  This script is meant to accompany some other kind of script that temporarily stores information in a file.</p>
<h5>How Cache Cleanup Works</h5>
<p>Here&#8217;s the <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/cleanup.txt' title='Cache Cleanup Script'>source code</a> in text format.</p>
<p>At the top of the script, you need to set two values &#8211; $dirName and $timeLimit.</p>
<p>$dirName should be a local path to the directory you want to scan for old files.  $timeLimit should be the maximum allowable inactive time, in seconds.</p>
<p>The logic of the script is very straightforward.  First, we <code>scandir()</code> to get an array of filenames in our cache directory.</p>
<p>Then, we loop through each fileName.  If the file was last accessed before our timeLimit, we delete the file.  If not, we leave the file alone.</p>
<p>If the script attempts to delete a file, it will output a success or failure message.  Otherwise, there is no output.</p>
<h5>How to Implement Cache Cleanup</h5>
<p>First, set the configuration variables to your desired settings.  I intentionally refrained from using any kind of input into the script for security reasons.</p>
<p>Next, decide how you want to run the script.</p>
<p>If your host allows you to run cron jobs, the logical choice would be to make this a cron job run every so often.  Depending on how long you want the temporary files to remain on your server, I&#8217;d run the cron job once a day or so.</p>
<p>If your host doesn&#8217;t allow cron jobs, you could manually access the script each time you want to check for idle files and delete them.  This isn&#8217;t a bad choice if you want to check once a week or so, but it is kind of annoying to do it every day.</p>
<p>You could also incorporate this into your caching script.  Every time the script creates or accesses a cached file, it could call this script (as a function) and delete unused files.  This may not be a desirable option for performance reasons if you have a lot of cached files, but it wouldn&#8217;t be bad for smaller cache directories.</p>
<p>Anyhow, I just wrote <a href='http://www.earn-web-cash.com/wp-content/uploads/2008/02/cleanup.txt' title='Cache Cleanup Script'>the script</a>.  You figure out how you want to implement it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$dirName</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'cache'</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// Path to the cache directory</span>
<span style="color: #000088;">$timeLimit</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">300</span><span style="color: #339933;">;</span>	<span style="color: #666666; font-style: italic;">// Max time since a file has been accessed</span>
			<span style="color: #666666; font-style: italic;">//   before it should be deleted</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$files</span> <span style="color: #339933;">=</span> <span style="color: #990000;">scandir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$files</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'.'</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$file</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'..'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">fileatime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$timeLimit</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirName</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$file</span> deleted successfully.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$file</span> not deleted.&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;br /&gt;'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/scripts-plugins-and-modules/cache-cleanup-script-php/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Create an RSS Feed for Your Site in PHP</title>
		<link>http://www.earn-web-cash.com/2008/02/14/create-rss-feed-php/</link>
		<comments>http://www.earn-web-cash.com/2008/02/14/create-rss-feed-php/#comments</comments>
		<pubDate>Thu, 14 Feb 2008 23:31:48 +0000</pubDate>
		<dc:creator>Walkere</dc:creator>
				<category><![CDATA[PHP Tutorials]]></category>
		<category><![CDATA[Web Design]]></category>
		<category><![CDATA[Feed]]></category>
		<category><![CDATA[file]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rss]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.earn-web-cash.com/2008/02/14/create-rss-feed-php/</guid>
		<description><![CDATA[Most blogging platforms come with built-in support for RSS feeds.  Why?  It's a great way for users to keep up to date on your new content.  Instead of loading up a dozen favorite sites, they can look at one feed reader and then decide what to pursue further.

So what if you don't use a blogging platform or CMS that creates a feed for you?  You can create one yourself in PHP.  It's pretty simple.]]></description>
			<content:encoded><![CDATA[<p><a href="http://life-of-brian.com/?attachment_id=614"><img class="alignright" title="Globe and Keyboard" src="http://life-of-brian.com/wp-content/uploads/2008/12/1097852_95279728-300x199.jpg" alt="Picture of a globe in front of a keyboard." /></a>Note: If you look around the site, you may have noticed that I haven&#8217;t updated this place in a long time. Check out my new project, which is more closely focused on <a href="http://digital-photography-howto.com">photography</a>. I still discuss some design issues, though, like these <a href="http://digital-photography-howto.com/free-indesign-templates/">free InDesign templates</a>. If you&#8217;re looking for a camera, check out this post on the <a href="http://digital-photography-howto.com/canon-t1i-to-t2i-to-t3i-what-digital-slr-camera-to-buy/">differences between entry level Canon dSLR cameras</a>.</p>
<p>Or, take a look at my <a href="http://olinda-gibbons.com">photography studio&#8217;s website</a>. Along with photography, we offer a handful of <a href="http://olinda-gibbons.com/print/">design and printing services</a>, mostly for models, actors, and fashion designers. Our newest offering is the <a href="http://olinda-gibbons.com/print/info-on-comp-zed-cards/">design and printing of modeling/acting comp cards</a>, which start at $60 for a set.</p>
<p>Most blogging platforms come with built-in support for RSS feeds.  Why?  It&#8217;s a great way for users to keep up to date on your new content.  Instead of loading up a dozen favorite sites, they can look at one feed reader and then decide what to pursue further.</p>
<p>So what if you don&#8217;t use a blogging platform or CMS that creates a feed for you?  You can create one yourself in PHP.  It&#8217;s pretty simple.<br />
<span id="more-112"></span></p>
<h5>Elements of an RSS 2.0 Feed</h5>
<p>There are pretty strict standards about RSS feeds &#8211; intended to make it easier for readers to parse and display the information.  Before we dive into building a feed, we should look at what it should contain.  You can read the <a href="http://feedvalidator.org/docs/rss2.html" title="RSS Feed 2.0 Specification">full spec at feedvalidator</a>.</p>
<p>An RSS 2.0 Feed is a special type of XML file.  The first things you&#8217;ll need in your file is an xml declaration, and an opening/closing RSS tag.  Like this.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;2.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  //  Feed elements here
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Inside the opening rss tag, you can also include any <a href="http://validator.w3.org/feed/docs/howto/declare_namespaces.html">namespaces</a> that you&#8217;ll be using.  That&#8217;s a bit more than we need to worry about at the moment, though.</p>
<p>Inside the rss tag, you need to create a channel tag.  This holds all of the individual articles as well as some basic information about the feed.  You&#8217;re required to create a <strong>title</strong>, <strong>link</strong>, and <strong>description</strong> tag.  You can add a lot of optional tags, of which the <strong>lastBuildDate</strong> tag would be most useful.</p>
<p>A basic channel declaration would look something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My Wonderful Blog<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://mydomain.com<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>This is a feed of the latest articles at My Wonderful Blog.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;lastBuildDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Thu, 14 Feb 2008 10:57:05 GMT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/lastBuildDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
   //  Include individual articles here
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/channel<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Finally, you can include the individual articles inside of this channel tag.  Each article is represented by an item tag and a series of optional tags.  Some good tags to use for a basic feed would be <strong>title</strong>, <strong>link</strong>, <strong>description</strong>, and <strong>pubDate</strong>.  Like this&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>My Latest Article<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/title<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://mydomain.com/latestarticle.php<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/link<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>This is the latest cool article at my wonderful blog.<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/description<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Thu, 14 Feb 2008 10:57:05 GMT<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/pubDate<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/item<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h5>Using PHP to Create the RSS Feed</h5>
<p>Now that we know what goes <strong>in to</strong> an RSS feed, we can worry about how to <strong>create</strong> one.</p>
<p>There are several approaches we can take here.  We could have PHP dynamically output an RSS feed every time the file is accessed.  We could create a SimpleXML object and write it to a file each time a new article is added.  Or we could simply create a string and write it to an xml file.</p>
<p>We&#8217;ll take the third approach.  This means we need to do three things.</p>
<ul>
<li>Fetch the data from the database</li>
<li>Build the xml formatted string</li>
<li>Write the xml data to a file</li>
</ul>
<h5>Fetch the Data from the Database</h5>
<p>This will vary depending on the database type you&#8217;re using and how it&#8217;s set up.  If you&#8217;ve gotten this far in creating a dynamic, PHP-driven site, I&#8217;m sure you can figure out how to fetch information for your own database.</p>
<p>For the purpose of this tutorial, we&#8217;ll fetch information from a mySQL database.  The database will hold the title, link, description, and pubDate of each article.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT title, link, description, pubDate FROM
   articles LIMIT 10&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note:  I only retrieved the last 10 entries.  There is no strict limit to the number of entries an RSS 2.0 feed can have, but it&#8217;s polite to leave it small.  If they want to read every article, they&#8217;ll come to your site.</p>
<h5>Building the XML Formatted String</h5>
<p>Now, we need to create a blank string, format it like an XML file, loop through the mySQL result array, and insert the data into the XML file.</p>
<p>First, we&#8217;ll add the xml declaration, the rss tag, the channel tag, and the channel info.  Then we can loop through the result set and create one item tag for each article.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">$xmlString = '<span style="color: #000000; font-weight: bold;">&lt;?</span>xml version<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1.0&quot;</span> encoding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;utf-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;rss version=&quot;2.0&quot;&gt;
&lt;channel&gt;
   &lt;title&gt;My Wonderful Blog&lt;/title&gt;
   &lt;link&gt;http://mydomain.com/&lt;/link&gt;
   &lt;description&gt;Latest articles at My Wonderful Blog.&lt;/description&gt;
   &lt;lastBuildDate&gt;' . date(&quot;D, d M Y H:i:s e&quot;) . '&lt;/lastBuildDate&gt;
&nbsp;
';
&nbsp;
while ($row = mysql_fetch_array($result)) {
$xmlString .= '   &lt;item&gt;
      &lt;title&gt;' . $row['title'] . '&lt;/title&gt;
      &lt;link&gt;' . $row['link'] . '&lt;/link&gt;
      &lt;description&gt;' . $row['description'] . '&lt;/description&gt;
      &lt;pubDate&gt;' . date(&quot;D, d M Y H:i:s e&quot;, $row['pubDate'] . '&lt;/pubDate&gt;
   &lt;/item&gt;
';
}
&nbsp;
$xmlString .= '&lt;/channel&gt;
&lt;/rss&gt;';</pre></div></div>

<p>Most of that should be self-explanatory.  Where I added a newline before ending the string, I was simply adding white space to make the source code more readable.  This isn&#8217;t necessary &#8211; but I like neat, readable source code.</p>
<p>$xmlString is being created in the beginning.  Then, the additional strings are appended to it with the .= operator.</p>
<p>The one important thing to note here is <code>date("D, d M Y H:i:s e")</code>.  This is PHP&#8217;s function to create a formatted date.  The characters inside the string determine how the timestamp is formatted.</p>
<p>This format is the standard required by RSS 2.0 feeds.  You could simply copy and paste this string to format your dates.  &#8220;D, d M Y&#8221; creates something like &#8220;Thu, 14 Feb 2008.&#8221;  &#8220;H:i:s&#8221; creates the time as &#8220;18:23:30.&#8221;  &#8220;e&#8221; is a representation of the timezone &#8211; which should be GMT.</p>
<h5>Write the String to an XML File</h5>
<p>Finally, we need to write this string to an xml file.  For simplicity&#8217;s sake, we&#8217;ll call this &#8220;feed.xml&#8221; and place it in the same directory as our script.</p>
<p>You could use a few functions to do the file-writing.  In this case, <code>file_put_contents</code> would be simplest &#8211; all we need to do is dump the contents of the string into a file and overwrite any previous contents.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;feed.xml&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;">$xmlString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now you just need to incorporate this into your existing site.  You could make this a standalone script and access it every time you add new content to your site.</p>
<p>Or, if you&#8217;re using some kind of home-brewed CMS, you could wrap this in a function and call it right after you save the content of a new article.</p>
<p>Besides creating a custom feed for your own website, there are some other uses for this.  You could use it to create a feed of articles from diverse sources around the internet.</p>
<p>If you write at a variety of sites &#8211; like Helium, Associated Content, and Xomba &#8211; you can use this to create a unified feed.  Or, you could create a feed of only <strong>select</strong> articles from one of those sources.  You could also create a feed of simple data &#8211; not articles &#8211; like quotes, jokes, tips, etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.earn-web-cash.com/2008/02/14/create-rss-feed-php/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		</item>
	</channel>
</rss>

