Page 1 of 1

Loop through a directory of files

Posted: Sat Sep 16, 2006 10:54 am
by akimm
I understand glob() can pull up the actual files in a directory, but if I wanted to pulll the files only if the date on them were say, in-between, a three day range, could I use preg_match or some preg function.

Please do not write the code, I need to get better I will post my makeshift crap code later when its written, and see if anyone is kind enough to helpme debug it. Thank you for any help and/or suggestions you can provide.

Posted: Sat Sep 16, 2006 10:58 am
by feyd
no need for preg_*, filemtime()

Posted: Sat Sep 16, 2006 11:09 am
by Luke
dir is a pseudo-object oriented way to read files from a directory. It's kind of cool. Something you could also look into.

filetime!

Posted: Sat Sep 16, 2006 11:10 am
by akimm
Wohoo..
thanks!

Posted: Sat Sep 16, 2006 11:15 am
by Luke
:wink: :D

Posted: Sat Sep 16, 2006 11:42 am
by wtf

Posted: Sat Sep 16, 2006 12:26 pm
by Luke
hey that looks pretty slick... I'll have to check that out. :D

Yea

Posted: Sat Sep 16, 2006 3:37 pm
by akimm
That really is a nice code. However it kinda goes away from my exact purpose, plus i'd rather try to do this myself, at least a draft of the code. I got something that i'm going to post up in a second here.

Date comparison question

Posted: Sat Sep 16, 2006 4:15 pm
by akimm
Ok, my purpose for this code is to read $path variable, enter that directory, view the creation time of the files, if they are lesser than 6 days I want them to echo out with the foreach loop that will then print new articles.

this is the error I'm getting
"Parse error: parse error, unexpected $ in /nfs/cust/8/25/05/650528/web/toop.php on line 21"

toop.php

Code: Select all

<table>
<tr>
<?php
$path = "advertisements/";
if (is_dir("$path")) 
       { 
           $handle = opendir($path); 
           while (false !== ($file = readdir($handle))) { 
               if ($file != "." && $file != "..")  {  
                   $Diff = (time() - filectime("$path/$file"))/60/60/24;
                   if ($Diff > 6) {
	foreach($path/$file as $key) {
	echo "<td>" . "new articles" . $key . "</tr>"; 
		  }
               } 
           }
           closedir($handle); 
       } 
 ?>
</tr></table>