<?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; delete</title>
	<atom:link href="http://www.earn-web-cash.com/tag/delete/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>
	</channel>
</rss>

