My function not find files to delete

My function not find files to delete

Post by Paol » Sun, 19 Apr 2009 22:58:06


Hi ar all

in my web site root I have 2 directories:

Firstone is named:pdf

Into the second I have a php file conteining this function:

function CleanFiles($dir)

{

//Delete temporary files

$t = time();

$h = opendir($dir);

while($file=readdir($h))

{

if(substr($file,0,3)=='tmp' and substr($file,-4)=='.pdf')

{

$path = $dir.'/'.$file;

if($t-filemtime($path)>3600)

@unlink($path);

}

}

closedir($h);

}

When I call this function with CleanFiles("../pdf") it'ld be delete all
temporany old files

but it does'nt find old files and it does'nt delete them.

Please can you take a look if there are errrors?

Thankyou in advance
 
 
 

My function not find files to delete

Post by Curtis Dye » Mon, 20 Apr 2009 12:54:04


Check if the directory stream was actually opened first.


It's a good idea to test if readdir() is not identicle to boolean
false. You might get a filename returned that evaluates to false
in an expression:

while (($file = readdir($h)) !== false)
{
...


It's usually a good idea to use && and || since the precedence of
these is more natural and intuitive in most cases.


You might want to normalize the path data.


The @ error suppressor is hiding the error that might explain why
you can't delete your files! This is not the right way to handle
it:

/* Check if the file exists */
if (file_exists($path)) {
unlink($path);
}
else {
/* There was an error, maybe log it or display it. */
}


You might echo out the contents of your variable `$path' to see if
the path looks correct to you.


Stop using the @ error suppressor when you need the information the
error can provide to help you.


Remove the @ error suppressor, and you can find the errors
yourself. There have been several discussions on this recently.

--
~Curtis
$email = str_replace('sig.invalid', 'gmail.com', $from);

Anonymous (1984 IOCCC winner):
int i;main(){for(;i["]<i;++i){--i;}"];read('-'-'-',i+++"hell\
o, world!\n",'/'/'/'));}read(j,i,p){write(j/p+p,i---j,i/i);}