To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > General Help

General Help Forum for General Help questions pertaining to PHP

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-03-2009, 01:27 PM   #1
marcnyc
Senior Member
 
Join Date: May 2002
Posts: 476
help with cache file-deleting script

Hello, I am trying to make a script that I can run via Cron to delete all the files in a specific cache folder.
I have this:

PHP Code:
<?php
$files
= array();
$index = array();
$yesterday = strtotime('yesterday');

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

foreach(
$index as $i => $t) {
    if(
$t < $yesterday) {
        @
unlink('cache_chaindlk/'.$files[$i]);
    }

}
?>
Doesn't seem to work.
marcnyc is offline   Reply With Quote
Old 11-03-2009, 02:19 PM   #2
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 3,099
try:
PHP Code:

$dir
= 'cache_chaindlk';//set to full server path
$seconds=  24 * 60 * 60;
$files= scandir($dir);
foreach(
$files as $num=>$fname){
    if(
file_exists($dir . $fname) && ((time() - filemtime($dir . $fname)) > $seconds)){
        
unlink($dir . $fname);
    
    }
}
dagon is offline   Reply With Quote
Old 11-03-2009, 03:08 PM   #3
marcnyc
Senior Member
 
Join Date: May 2002
Posts: 476
I added a few slashes and did this:

PHP Code:
$dir= getcwd().'/cache_chaindlk';//set to full server path
$seconds= 24 * 60 * 60;
$files= scandir($dir);
foreach(
$files as $num=>$fname){
    if(
file_exists($dir.'/'.$fname) && ((time() - filemtime($dir.'/'.$fname)) > $seconds)){
        
unlink($dir.'/'.$fname);
        echo
'Deleting '.($dir.'/'.$fname).'...<br />';
    }
}
Seems to be working, if it doesn't I'll report back in half hour when it's goes through all 5000 cache files...


Thanks for your help!
marcnyc is offline   Reply With Quote
Old 11-03-2009, 03:16 PM   #4
dagon
RTFM it's a way of life.
 
dagon's Avatar
 
Join Date: Nov 2001
Location: N.Z
Posts: 3,099
PHP Code:
$dir= getcwd().'/cache_chaindlk/';
will save you adding a / 4 times

the code i posted i grabbed from from one of my files and i dent look at it to carefully :-)
dagon is offline   Reply With Quote
Old 11-03-2009, 03:19 PM   #5
marcnyc
Senior Member
 
Join Date: May 2002
Posts: 476
you are right about that!
marcnyc is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:44 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.