PDA

View Full Version : Sort files by date/time.


Darknight
05-22-2003, 02:37 PM
I have a webcam that takes pictures 2 times a day. The file names are 000image.jpg - 999image.jpg.
I would like to put a .php file in the dir where the jpg's are stored that will list the images in the order they were made.
I thought about trying to use the numbers on the file name but if the cam is at 950 and takes 100 snap shots in one session they would end up being 950 - 050. Viewing these by number out put them all out of order.

Is there a way to load the file names into an array and then sort them by the date/time stamp?

thox
05-22-2003, 02:54 PM
You could use opendir() (http://uk.php.net/manual/en/function.opendir.php) to create your array. Then you could use filemtime() (http://uk.php.net/manual/en/function.filemtime.php) to check when each file was last modified. Sort the array and display as required.

Darknight
05-22-2003, 09:11 PM
ok so far i can get the date and filename into vars...
How do I get these into the array?

As I loop over each file I would like to fill an array with only the files for a specific day.

Right now as it loops through the while statment, it replaces the values in the array with new values... not appending them to the end.

Here is the code:

<!DOCTYPE HTML PUBLIC "-//Microsoft/DTD Microsoft Internet Explorer 3.01 HTML//EN">

<html>
<head>
<title>Array test</title>
</head>
<?php
$d = dir("C:\Inetpub\wwwroot");
$Moncount = 0;
$Tuecount = 0;
$Wedcount = 0;
$Thucount = 0;
$Fricount = 0;
$Satcount = 0;
$Suncount = 0;
echo "Handle: ".$d->handle."<br>\n";
echo "Path: ".$d->path."<br>\n";
while (false !== ($entry = $d->read())) {
if($entry !== "." & $entry !== ".." & substr("$entry", -3,3)=="jpg")
{
echo "<br>";
$s = filectime("C:\\Inetpub\\wwwroot\\".$entry);
//echo $s;
$day = date("D", $s);
$date = date("D h A", $s);


if($day == "Mon")
{
$monarray = array("$entry" => "$day");
$Moncount = $Moncount+1;
}

if($day == "Tue")
{
$Tuecount = $Tuecount+1;
}

if($day == "Wed")
{
$Wedcount = $Wedcount+1;
}

if($day == "Thu")
{
$thuarray = array("$date" => "$entry");

while (list ($key, $val) = each ($thuarray)) {
echo "$key = $val<br>\n";
}
$Thucount = $Thucount+1;
}

if($day == "Fri")
{
$Fricount = $Fricount+1;
}

if($day == "Sat")
{
$Satcount = $Satcount+1;
}

if($day == "Sun")
{
$Suncount = $Suncount+1;
}



//echo $entry."-----";
echo("Updated on " . $date . "----" . $day ."\n");

}
}
$d->close();
echo "<br><br>";
echo "<a href=\"http://localhost/imagelist.php?Day=Mon\">Mon:</a>".$Moncount."<br>";
echo "<a href=\"http://localhost/imagelist.php?Day=Tue\">Tue:</a>".$Tuecount."<br>";
echo "<a href=\"http://localhost/imagelist.php?Day=Wed\">Wed:</a>".$Wedcount."<br>";
echo "<a href=\"http://localhost/imagelist.php?Day=Thu\">Thu:</a>".$Thucount."<br>";
echo "<a href=\"http://localhost/imagelist.php?Day=Fri\">Fri:</a>".$Fricount."<br>";
echo "<a href=\"http://localhost/imagelist.php?Day=Sat\">Sat:</a>".$Satcount."<br>";
echo "<a href=\"http://localhost/imagelist.php?Day=Sun\">Sun:</a>".$Suncount."<br>";
echo "<br>";

arsort($thuarray);
//reset($thuarray);


while (list ($key, $val) = each ($thuarray)) {
echo "$key = $val\n";
}

?>
</body>
</html>

OUTPUTS:

Handle: Resource id #1
Path: C:\Inetpub\wwwroot

Thu 02 PM = 1.jpg
Updated on Thu 02 PM----Thu
Thu 02 PM = 2.jpg
Updated on Thu 02 PM----Thu
Thu 03 PM = 3.jpg
Updated on Thu 03 PM----Thu

Mon:0
Tue:0
Wed:0
Thu:3
Fri:0
Sat:0
Sun:0