Page 1 of 1

php is erasing all files not only *.dat

Posted: Tue Jan 09, 2007 6:09 am
by methos
Why does this code not only erases all .dat files but also all the files under that directory such as a file called "empty" (with no file extension)? ($sessionid is created using random md5) I only want it to erase all .dat files.

Code: Select all

// destroy previous sessions
$sessionfilename = ".$sessionid.".dat;
  foreach (glob("./sessions/*") as $sessionfilename) {
   if (filemtime($sessionfilename) + 604800 < time()) {
     unlink($sessionfilename);
   }
  }

Re: php is erasing all files not only *.dat

Posted: Tue Jan 09, 2007 7:38 am
by timvw
methos wrote:Why does this code not only erases all .dat files but also all the files under that directory such as a file called "empty" (with no file extension)? ($sessionid is created using random md5) I only want it to erase all .dat files.

Code: Select all

// destroy previous sessions
$sessionfilename = ".$sessionid.".dat;
  foreach (glob("./sessions/*") as $sessionfilename) {
   if (filemtime($sessionfilename) + 604800 < time()) {
     unlink($sessionfilename);
   }
  }
You are globbing over ./session/* -> and each file that is found is stored in $sessionfilename in the foreach construct.... and then you unlink $sessionfilename...

Code: Select all

foreach(glob("xxx/*.dat") as $foundfile) { echo $foundfile; }

Posted: Tue Jan 09, 2007 9:54 am
by Kieran Huggins
Also, I could be crazy..... but shouldn't this be:

Code: Select all

$sessionfilename = $sessionid.".dat";

Posted: Tue Jan 09, 2007 11:39 am
by m3mn0n
Nah, you're not crazy. That's a syntax error and you corrected it. :)