Forum Moderators: coopster

Message Too Old, No Replies

Delete temp files

How to delete temp files - GD gallery

         

tomda

8:26 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

I have few GD galleries which work fine. The only problem are the temp files. Before processing pictures, they are copy in my temp directory (as a preview).

Is there a way to have a script which will delete temp files which are note dated from today's date. It seems that $_files[date] do not exist? Any help?

Thank you

tomda

10:01 am on Jun 18, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a script which use the filemtime() function.
Almost done, but there are one problem :
- current time shown is not the real current time (there is a difference of 12 days?)

Just copy the script below and paste it in any directory.

Could someone guide me and let me know why current time is wrong?

Thank you

********************************
Here is the script
Note that script between MODIFICATION have been added by me

<?
/*
PHP Directory display<br/>
Copyright (C) 2002 Richard Clark (rclark@exorsus.net)

See the GNU General Public License included with this software
for more details. If none is included you have NO LICENSE to
operate this software and should contact the author immediately.

*/
function format_size($size) {
if ($size<1024) { return $size."b"; }
if ($size<(1024*1024)) { return sprintf("%dk",$size/1024); }
return sprintf("%.1fM",$size/(1024*1024));
}

function get_dir_listing() {
$listing = array();
$dh = opendir(getcwd());
while ($file = readdir($dh)) {
array_push($listing, $file);
}
closedir($dh);
sort($listing);
return $listing;

}

?>
<html>
<head>
<title>Listing of <?=dirname($PHP_SELF)?></title>
<style>
body { background-color: white; color: #000066; }
a { color: #444444; text-decoration: none; }
table,td,tr { spacing: 0; padding: 0; }
.dir a { color: #aa0000; }
</style>
</head>
<body>
<pre>
<?
//MODIFICATION
echo "Current time&nbsp;&nbsp;-&nbsp;&nbsp;File time&nbsp;&nbsp;-&nbsp;&nbspDifference(second)&nbsp;&nbsp;-&nbsp;&nbsp;File name<br><br>";
//END MODIFICATION

foreach (get_dir_listing() as $file) {
$name = preg_replace("/\s/","%20",$file);
$name = preg_replace("/'/","%27",$name);

$time = date("M m H:i",filemtime($file));
if ($file == ".") continue;
if ($file == "index.php") continue;
if ($file == ".htaccess") continue;
if ($file == "..")
//MODIFICATION
continue;
//END MODIFICATION
//$name="Parent Directory";

//MODIFICATION
if ($file!= "." && $file!= ".htaccess" && $file!= ".." && $file!= "index.php")
{$Diff = (time() - filemtime($file));
echo "".date('M m H:i',time())."&nbsp;&nbsp;-&nbsp;&nbsp;"; //CURRENT DATE
echo $time."&nbsp;&nbsp;-&nbsp;&nbsp;"; // FILE DATE
echo $Diff; // DIFFERENCE
if ($Diff > 86400) //24*60*60
{ echo " (<input type='checkbox' name='delete[]' checked>OLD)&nbsp;&nbsp;-&nbsp;&nbsp;";} else {echo " (new)&nbsp;&nbsp;-&nbsp;&nbsp;";}
}
//END MODIFICATION

if (is_dir($file))
printf("<span class=dir><a href='%s/'>%-60.60s</a> %s %6.6s</span>\n",$file,"$name/",$time,"-");
else
printf("<a href='%s'>%-60.60s</a> %s %6.6s\n", $name,$file,$time,format_size(filesize($file)));
}
?>
</pre>
</body>
</html>

WhosAWhata

6:51 pm on Jun 21, 2004 (gmt 0)

10+ Year Member



take a look at the following functions
FileMTime [php.net]
FileATime [php.net]
FileCTime [php.net]

then you could make a function like

function delete_old($dir){
if($dir[(strlen($dir)-1)]!= "/") $dir .= "/"; //check for trailing slash
$d = dir($dir);
$date = date(U);
$html = "Results of delete old files in ".$dir.":<br><br>";
while($f = $d->read()){
$date2 = fileMtime($dir.$f); //change to A C or M
if(($date - $date2) > 60*60*24) { // 60secs per min*60min per hr*24hr per day change to your time
$html .= $f.": Delete <font color=\"";
if(unlink($f)) {
$html .= "green\">Success";
} else {
$html .= "red\">Failed";
}
$html .= "</font><br>";
} else {
$html .= $f.": Leave Alone<br>";
}
}
$d->close();
return $html;
}

//then call

echo delete_old("myDIR");

tomda

9:23 am on Jun 22, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks WhosAWhata for giving me an alternative script, that's great!

In fact, I had worked on the first script this week-end and found the error. For those who read this thread and want to use the first script, then you should change de

date (M m ...

by

date (M d ...

BTW : The owner of the script has been informed of the error and it is now fixed.

[edited by: jatar_k at 3:59 pm (utc) on June 22, 2004]
[edit reason] removed url [/edit]