Loop through a directory of files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Loop through a directory of files

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

no need for preg_*, filemtime()
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

filetime!

Post by akimm »

Wohoo..
thanks!
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

:wink: :D
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Post by Luke »

hey that looks pretty slick... I'll have to check that out. :D
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Yea

Post 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.
User avatar
akimm
Forum Contributor
Posts: 460
Joined: Thu Apr 27, 2006 10:50 am
Location: Ypsilanti Michigan, formally Clipsburgh

Date comparison question

Post 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>
Post Reply