|
|
| Author |
Message |
| powelly |
This post is not being displayed .
|
 powelly World Chat Champion

Joined: 29 May 2007 Karma :  
|
 Posted: 15:37 - 06 Feb 2009 Post subject: [PHP] display files in order |
 |
|
I have a webcam that uploads an image to a folder every minute, this files are named as follows:
snapshot-2009-02-02-18-25-02.jpg
Im using the following code to display all the images from a given date (currently hardcoded as $date)
| Code: | <?php
$date = "2009-02-02";
if ($handle = opendir('../../webcam/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (eregi('2009-02-02', $file)) {
$page_name = $file;
//$page_name=substr($file, 0, strpos($file, "."));
echo "<img src='../../webcam/$page_name' width='160'/>";
}
}
}
}
closedir($handle);
?> |
However the output does not display the images in order, is there anyway to get round this based on either the filename or the files timestamp? ____________________ Yamaha RXS 100 > Gilera Runner 50 > Suzuki SV650SY |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
 Posted: 15:59 - 06 Feb 2009 Post subject: |
 |
|
Hi
I would do your directory command and stick the results into an array and then look through the array to output it.
Eg.
| Code: |
<?php
$date = "2009-02-02";
$pages=array();
if ($handle = opendir('../../webcam/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (eregi('2009-02-02', $file)) {
$pages[] = $file;
}
}
}
}
rsort($pages);
foreach($pages as $page_name)
{
echo "<img src='../../webcam/$page_name' width='160'/>";
}
closedir($handle);
?>
|
Not tried test running it but that should work.
All the best
Keith ____________________ Traxpics, track day and racing photographs - Bimota Forum - Bike performance / thrust graphs for choosing gearing |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| rc211v |
This post is not being displayed .
|
 rc211v Nitrous Nuisance

Joined: 08 Oct 2007 Karma :     
|
 Posted: 16:27 - 06 Feb 2009 Post subject: |
 |
|
| Code: | | while (false !== ($file = readdir($handle))) |
that's a weird way of constructing a conditional statement.
I always advice my students from doing it this way, I don't know why though.
Anyway, yeah, same as mentioned above. Go through the directory and place the filenames into an array. You can then sort the array, and use it to display your images in order.
I don't know if PHP comes with a built-in sorting function, but there are lots of algorithms out there you can use. Bubble sort maybe?  ____________________ VFR400 (NC30) |
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| Kickstart |
This post is not being displayed .
|
 Kickstart The Oracle

Joined: 04 Feb 2002 Karma :     
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
| powelly |
This post is not being displayed .
|
 powelly World Chat Champion

Joined: 29 May 2007 Karma :  
|
|
| Back to top |
|
You must be logged in to rate posts |
|
 |
Old Thread Alert!
The last post was made 17 years, 232 days ago. Instead of replying here, would creating a new thread be more useful? |
 |
|
|