Jump to content

Searching & sorting files in a directory using DIR function


jetsettt

Recommended Posts

PHP version 5.

 

What I'm trying to do

List the contents of a folder that only contains FTP uploaded jpeg files from a camera. List by the newest file first and also possibly manipulate the output using a search variable such as the date created.

 

The code I currently have working

 

<select name="ImageFile">
<option value="">
<?php
$dirPath = dir('ftp_images/');
$imgArray = array();
while (($file = $dirPath->read()) !== false)
{
  if ((substr($file, -3)=="jpg"))
  {
     $imgArray[ ] = trim($file);
  }
}
$dirPath->close();
sort($imgArray);
$c = count($imgArray);
for($i=0; $i<$c; $i++)
{
    echo "<option value=\"" . $imgArray[$i] . "\">" . $imgArray[$i] . "\n";
}
?>
</select>

 

Problem

I want to view these images in order of date & time uploaded. Reading files from a filesystem and manipulating the results is not something I know much about. I have done alot of Googling and I seems a very in depth subject and i'm worrying I cannot go any further with this. I am thinking that filemtime() is the way to get the file date & time but I don't know how to combine it with the code above and sort by newest file first?

 

Can anyone point me in the right direction. Thanks in advance.

 

I'm slowly getting there. Managed to list by date (newest first). Found this code on here. Works very well... but

 

<?php

      $arr = array();

      foreach (glob("ftp_images/*.jpg") as $filename) 
      {
         $arr[$filename] = filemtime($filename);
       //echo $filename . " was last modified on " . date('M d Y, h:i:m', filemtime($filename)) . "<br>";    
      }

   ///
   
   //asort($arr);
   //echo "<br>";

   //foreach($arr as $key => $value) 
      //{
    //   echo "$key was last modified on " . date('M d Y, h:i:m', $value) . "<br>";
      //}
   
   ///
   
      arsort($arr);
      echo "<br>";

      foreach($arr as $key => $value) 
   {
       echo $key . " - " . date('d-m-Y H:i:s', $value) . "<br>";
      } 
?>

 

Agghhh...the script is timing out as there are possibly too many image files... approx 9,000 of them.

 

Does anyone know how to limit output to 200 image files that are brought back from a POST variable but still keep the files in order by date & time?

 

Thanks in advance...

 

 

 

It sounds like you're looking for pagination. For array-pagination you basically take the entire array of filenames and walk through it using offsets.

 

Basically, this is the procedure:

1) Get the total number of elements in the array (using count -- I'll call it $count) and decide how many elements you want to display on each page (I'll call it $perpage)

2) Get the number of pages needed to display all the filenames by dividing $count by $perpage and round-up using ceil. (If you round down, some elements of the array will not get displayed if $count and $perpage don't divide evenly). I'll call this $total_pages;

3) calculate the offset based on the page-number ($_GET['page'] -- comes from http://foo.bar/filenames.php?page=2) subtract 1 (so it aligns correctly with the array-keys of the array you're paginating) and multiply by $perpage (to get the page numbers as opposed to just number of array-elements). I'll call this $offset.

4) use array_slice to "walk" through the array-elements.

    array_slice($array, $offset, $perpage); ($array is the array of filenames)

 

Next...

We're not done yet, you have to generate links for the pages

<?php
    $page = $_GET['page'];
    $query_string = $_SERVER['query_string'];
    if(stristr($query_string, 'page=') === false){
        $query_string .= strlen($query_string) > 0 ? '&page=1' : 'page=1';
        $page = 1;
    }
    $links = array();
    for($i = 1; $i <= $total_pages; ++$i){
        if($i == $page)
             $links[] = $i;
        else
             $links[] = '<a href="?'.str_replace('page='.$page, 'page='.$i, $query_string).'">'.$i.'</a>';
    }
    $output = implode(', ', $links);
?>

I just provided the code for that as I was getting tired of verbalizing the process. That should work.

Thanks again Goldeneye.

 

I ended up with the code below that worked a treat!

 

<?php $array = array();
  foreach (glob("ftp_images/*.jpg") as $filename) 
      {
         $array[$filename] = filemtime($filename);
      }
      asort($array);  // Sort an array in order and maintain index association
  $count = count($array);  // Count how many files in the array
  $perpage = 200;  // No of files to display per page
  $pageresult = $count / $perpage;   // Calculate how many pages are required. $count is divided by $perpage
  $total_pages = ceil($pageresult);  // Use ceil() to roundup to the highest whole number to display ALL pages 
  $offset = $_GET['page'] - 1;  // Get the page numbers. Set -1 for the correct offset for the array
  $array = array_slice($array, $offset, $perpage); ?>

 

I can go through the files now with $_GET['page'] and limit how many files I can display!

 

I have learned some pagination that will be very useful. :D

 

Thankyou!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.