<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Deleting Files In A Directory Older Than Today Every Week With PHP</title>
	<atom:link href="http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/feed/" rel="self" type="application/rss+xml" />
	<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/</link>
	<description>Coding Tutorials, hints &#38; tips in PHP, jQuery, CSS &#38; HTML</description>
	<lastBuildDate>Tue, 07 Sep 2010 01:40:56 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Veneficus Unus</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6998</link>
		<dc:creator>Veneficus Unus</dc:creator>
		<pubDate>Wed, 15 Apr 2009 03:03:51 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6998</guid>
		<description>Hi,

Creating a cron job is quite simple once you know how. It all depends on your server config. Usually (if there isn&#039;t a way to do it on your host providers website) you log in to your server using shell access (SSH) using a program like Putty.

Once you&#039;re there type crontab -e &amp; the server should automatically create a new cronjob for you. Then to set it up so the script runs every day at 1 in the morning type this in the file:
[bash]
0 1 * * * /full/nix/path/to/php/binary -q /full/nix/path/to/php/script.php
[/bash]
Once you are done hit Ctrl+o then Ctrl+x that saves &amp; exits.

I hope that helps. If not let me know who you are hosted on &amp; if they give you SSH access (if you don&#039;t know you can always send them an email to ask).</description>
		<content:encoded><![CDATA[<p>Hi,</p>
<p>Creating a cron job is quite simple once you know how. It all depends on your server config. Usually (if there isn&#8217;t a way to do it on your host providers website) you log in to your server using shell access (SSH) using a program like Putty.</p>
<p>Once you&#8217;re there type crontab -e &amp; the server should automatically create a new cronjob for you. Then to set it up so the script runs every day at 1 in the morning type this in the file:</p>
<pre class="brush: bash;">
0 1 * * * /full/nix/path/to/php/binary -q /full/nix/path/to/php/script.php
</pre>
<p>Once you are done hit Ctrl+o then Ctrl+x that saves &amp; exits.</p>
<p>I hope that helps. If not let me know who you are hosted on &amp; if they give you SSH access (if you don&#8217;t know you can always send them an email to ask).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bridarian Jones</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6994</link>
		<dc:creator>Bridarian Jones</dc:creator>
		<pubDate>Wed, 15 Apr 2009 02:04:23 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6994</guid>
		<description>Hi

I need your help and looking at your post I see you know what you are talking about. I want to delete all files in my cache folder every day at 1 in the morning. It is located at /public_html/wp-content/themes/college/cache on my server. I don&#039;t understand how to create a cron job for this. Can you help me please?

Bridarian</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>I need your help and looking at your post I see you know what you are talking about. I want to delete all files in my cache folder every day at 1 in the morning. It is located at /public_html/wp-content/themes/college/cache on my server. I don&#8217;t understand how to create a cron job for this. Can you help me please?</p>
<p>Bridarian</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veneficus Unus</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6725</link>
		<dc:creator>Veneficus Unus</dc:creator>
		<pubDate>Wed, 08 Apr 2009 12:32:36 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6725</guid>
		<description>Np. :)

Thanks for the tips in making the code neater too. :D</description>
		<content:encoded><![CDATA[<p>Np. <img src='http://return-true.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Thanks for the tips in making the code neater too. <img src='http://return-true.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Todd Horst</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6723</link>
		<dc:creator>Todd Horst</dc:creator>
		<pubDate>Wed, 08 Apr 2009 12:27:15 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6723</guid>
		<description>Your right that is a mistake. 
$del_date=time()-(2 * 60 * 60);  

should be 
$del_date=time()-($ret_hr * 60 * 60); 

Also I changed
if ($handle = opendir($dir)) {

to
if (is_readable($dir) &amp;&amp; $handle = opendir($dir)) {

Thanks for this script though. It was just what i needed.</description>
		<content:encoded><![CDATA[<p>Your right that is a mistake.<br />
$del_date=time()-(2 * 60 * 60);  </p>
<p>should be<br />
$del_date=time()-($ret_hr * 60 * 60); </p>
<p>Also I changed<br />
if ($handle = opendir($dir)) {</p>
<p>to<br />
if (is_readable($dir) &amp;&amp; $handle = opendir($dir)) {</p>
<p>Thanks for this script though. It was just what i needed.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veneficus Unus</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6594</link>
		<dc:creator>Veneficus Unus</dc:creator>
		<pubDate>Sun, 05 Apr 2009 13:11:11 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6594</guid>
		<description>Yep, you&#039;re right. I just never realized I could have shortened it. The reason my code uses arrays is because I do other things with the file &amp; time info in another script I&#039;m using it in &amp; thought I&#039;d just use that part instead of writing it again.

Thanks for the improvement though. That&#039;s why I love the coding world. :) Although I believe there might be a mistake in your code. You require the setting of the variable &lt;code&gt;$ret_hr&lt;/code&gt; in the function call &amp; then never use it?</description>
		<content:encoded><![CDATA[<p>Yep, you&#8217;re right. I just never realized I could have shortened it. The reason my code uses arrays is because I do other things with the file &#038; time info in another script I&#8217;m using it in &#038; thought I&#8217;d just use that part instead of writing it again.</p>
<p>Thanks for the improvement though. That&#8217;s why I love the coding world. <img src='http://return-true.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Although I believe there might be a mistake in your code. You require the setting of the variable <code>$ret_hr</code> in the function call &#038; then never use it?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Todd Horst</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6583</link>
		<dc:creator>Todd Horst</dc:creator>
		<pubDate>Sun, 05 Apr 2009 01:24:51 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6583</guid>
		<description>Hey, not to bug you but couldn&#039;t you simplify this quite a bit?
[php]
/*
Cleanup a directory, deleteing any files older then set amount of hours.
@dir-     The directory to cleanup
@ret_hr-  Files older then this will be deleted

Call like-
  cleanup_dir(&quot;test_data/&quot;,24)
*/
function cleanup_dir($dir,$ret_hr) {
  $del_date=time()-(2 * 60 * 60);                                               //Get retention in epoch (hours * 60 min * 60 seconds)
   
  if ($handle = opendir($dir)) {                                                //Get a handle of the directory
      clearstatcache();                                                         //Clear cached dir data
      while (false !== ($file = readdir($handle))) {                            //If there are still files
        $t=filemtime( $dir.$file );                                             //Get the file time
        if ($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot; &amp; $t &lt; $del_date) {                   //If this is older then retention
            @unlink($dir.$file);
        }
      }
      closedir($handle);
  }
}
[/php]</description>
		<content:encoded><![CDATA[<p>Hey, not to bug you but couldn&#8217;t you simplify this quite a bit?</p>
<pre class="brush: php;">
/*
Cleanup a directory, deleteing any files older then set amount of hours.
@dir-     The directory to cleanup
@ret_hr-  Files older then this will be deleted

Call like-
  cleanup_dir(&quot;test_data/&quot;,24)
*/
function cleanup_dir($dir,$ret_hr) {
  $del_date=time()-(2 * 60 * 60);                                               //Get retention in epoch (hours * 60 min * 60 seconds)

  if ($handle = opendir($dir)) {                                                //Get a handle of the directory
      clearstatcache();                                                         //Clear cached dir data
      while (false !== ($file = readdir($handle))) {                            //If there are still files
        $t=filemtime( $dir.$file );                                             //Get the file time
        if ($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot; &amp; $t &lt; $del_date) {                   //If this is older then retention
            @unlink($dir.$file);
        }
      }
      closedir($handle);
  }
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veneficus Unus</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6456</link>
		<dc:creator>Veneficus Unus</dc:creator>
		<pubDate>Tue, 31 Mar 2009 13:55:04 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6456</guid>
		<description>No problem. Let me know how it works out &amp; if I can help anymore. :)</description>
		<content:encoded><![CDATA[<p>No problem. Let me know how it works out &amp; if I can help anymore. <img src='http://return-true.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Javan Jensen</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6455</link>
		<dc:creator>Javan Jensen</dc:creator>
		<pubDate>Tue, 31 Mar 2009 13:51:02 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6455</guid>
		<description>Thanks,
   I will give this a try.  Appreciate your help.</description>
		<content:encoded><![CDATA[<p>Thanks,<br />
   I will give this a try.  Appreciate your help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Veneficus Unus</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6267</link>
		<dc:creator>Veneficus Unus</dc:creator>
		<pubDate>Sun, 22 Mar 2009 23:28:59 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6267</guid>
		<description>Hey there. This code will kinda work. The main problem is that Unix doesn&#039;t actually have a file creation time only a time last modified or accessed. That is what I use on mine. Anyway the main problem is that when you create a folder or file inside a folder it changes the folders modified date.

I have wrote the following which will empty out all files (since folders can&#039;t be deleted with PHP without first deleting the files) from folders older than 3 days, then delete the mothly named folders older than 3 days &amp; then delete the cam folder if it is older than 3 days. You can stop the deletion of the cam folder if you comment out the 7 lines indicated in the code.

Just change the &lt;code&gt;$deletion_path&lt;/code&gt; variable to the full *nix path to the folder with the cam folders in. The full *nix path is usually something like &lt;code&gt;/mnt/home/username&lt;/code&gt; or something, and do not add a trailing slash (that&#039;s a slash on the end of the path). Anyway here is the code.
[php]
&lt;?php
//path to the folder that conatins the cam01, cam02 etc folders... WITHOUT trailing slash
$deletion_path = &#039;/full/path/to/folder&#039;;
$rm_time = strtotime(&#039;-3 day&#039;);

//Main function &amp; rest of code
function get_file_list($path) {
	$files = array();
	$index = array();
	
	if ($handle = opendir($path)) {
		clearstatcache();
		while (false !== ($file = readdir($handle))) {
			if ($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot;) {
				$files[] = $file;
				$index[] = filemtime( $path.&#039;/&#039;.$file );
			}
		}
		closedir($handle);
	}
	
	return array($files, $index);
}
$folders = get_file_list($deletion_path);

foreach($folders[0] as $folder) {
	$date_entries = get_file_list($deletion_path.&#039;/&#039;.$folder);
	asort($date_entries[1]);
	foreach($date_entries[0] as $date_folder) {
		$date_files = get_file_list($deletion_path.&#039;/&#039;.$folder.&#039;/&#039;.$date_folder);
		foreach($date_files[0] as $file) {
			if(!is_dir($deletion_path.&#039;/&#039;.$folder.&#039;/&#039;.$date_folder.&#039;/&#039;.$file)) {
				foreach($date_files[1] as $t) {
					if($t &lt; $rm_time) {
						@unlink($deletion_path.&#039;/&#039;.$folder.&#039;/&#039;.$date_folder.&#039;/&#039;.$file);	
					}
				}
			}
		}
		if(is_dir($deletion_path.&#039;/&#039;.$folder.&#039;/&#039;.$date_folder)) {
			foreach($date_entries[1] as $t_f) {
				if($t_f &lt; $rm_time) {
					@rmdir($deletion_path.&#039;/&#039;.$folder.&#039;/&#039;.$date_folder);
				}
			}
		}
	}
	//comment out the next 7 lines useing // if you don&#039;t want to delete the cam folders that the date folders are in after 3 days
	if(is_dir($deletion_path.&#039;/&#039;.$folder)) {
		foreach($folders[1] as $t_c) {
			if($t_c &lt; $rm_time) {
				@rmdir($deletion_path.&#039;/&#039;.$folder);
			}
		}
	}
}
?&gt;
[/php]</description>
		<content:encoded><![CDATA[<p>Hey there. This code will kinda work. The main problem is that Unix doesn&#8217;t actually have a file creation time only a time last modified or accessed. That is what I use on mine. Anyway the main problem is that when you create a folder or file inside a folder it changes the folders modified date.</p>
<p>I have wrote the following which will empty out all files (since folders can&#8217;t be deleted with PHP without first deleting the files) from folders older than 3 days, then delete the mothly named folders older than 3 days &#038; then delete the cam folder if it is older than 3 days. You can stop the deletion of the cam folder if you comment out the 7 lines indicated in the code.</p>
<p>Just change the <code>$deletion_path</code> variable to the full *nix path to the folder with the cam folders in. The full *nix path is usually something like <code>/mnt/home/username</code> or something, and do not add a trailing slash (that&#8217;s a slash on the end of the path). Anyway here is the code.</p>
<pre class="brush: php;">
&lt;?php
//path to the folder that conatins the cam01, cam02 etc folders... WITHOUT trailing slash
$deletion_path = '/full/path/to/folder';
$rm_time = strtotime('-3 day');

//Main function &amp; rest of code
function get_file_list($path) {
	$files = array();
	$index = array();

	if ($handle = opendir($path)) {
		clearstatcache();
		while (false !== ($file = readdir($handle))) {
			if ($file != &quot;.&quot; &amp;&amp; $file != &quot;..&quot;) {
				$files[] = $file;
				$index[] = filemtime( $path.'/'.$file );
			}
		}
		closedir($handle);
	}

	return array($files, $index);
}
$folders = get_file_list($deletion_path);

foreach($folders[0] as $folder) {
	$date_entries = get_file_list($deletion_path.'/'.$folder);
	asort($date_entries[1]);
	foreach($date_entries[0] as $date_folder) {
		$date_files = get_file_list($deletion_path.'/'.$folder.'/'.$date_folder);
		foreach($date_files[0] as $file) {
			if(!is_dir($deletion_path.'/'.$folder.'/'.$date_folder.'/'.$file)) {
				foreach($date_files[1] as $t) {
					if($t &lt; $rm_time) {
						@unlink($deletion_path.'/'.$folder.'/'.$date_folder.'/'.$file);
					}
				}
			}
		}
		if(is_dir($deletion_path.'/'.$folder.'/'.$date_folder)) {
			foreach($date_entries[1] as $t_f) {
				if($t_f &lt; $rm_time) {
					@rmdir($deletion_path.'/'.$folder.'/'.$date_folder);
				}
			}
		}
	}
	//comment out the next 7 lines useing // if you don't want to delete the cam folders that the date folders are in after 3 days
	if(is_dir($deletion_path.'/'.$folder)) {
		foreach($folders[1] as $t_c) {
			if($t_c &lt; $rm_time) {
				@rmdir($deletion_path.'/'.$folder);
			}
		}
	}
}
?&gt;
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Javan Jensen</title>
		<link>http://return-true.com/2009/03/deleting-files-in-a-directory-older-than-today-every-week-with-php/comment-page-1/#comment-6255</link>
		<dc:creator>Javan Jensen</dc:creator>
		<pubDate>Sun, 22 Mar 2009 17:40:01 +0000</pubDate>
		<guid isPermaLink="false">http://return-true.com/?p=623#comment-6255</guid>
		<description>This is almost what I need.  I have no knowledge on how to do this so would appreciate your help.  I have several directories which then have folders with the date as the name, examples 0322.  I want to be able to go into each directory, cam01, cam02, etc. and delete any folders that are 3 days old.  Would like to do this every day so I only have 2 days worth of data in each folder.  Can you help me with this?

Thanks</description>
		<content:encoded><![CDATA[<p>This is almost what I need.  I have no knowledge on how to do this so would appreciate your help.  I have several directories which then have folders with the date as the name, examples 0322.  I want to be able to go into each directory, cam01, cam02, etc. and delete any folders that are 3 days old.  Would like to do this every day so I only have 2 days worth of data in each folder.  Can you help me with this?</p>
<p>Thanks</p>
]]></content:encoded>
	</item>
</channel>
</rss>
