Jump to content

Filter directory JPG's using READDIR() and timestamp


jetsettt

Recommended Posts

Hi all,

 

I have a PHP function that reads JPG's from a folder that contains over 100,000 files. The problem I have is that the READDIR() function takes a while to list the contents due to the large number of files. I'm trying to speed up the process by using a filter and listing only the files I need. Example: list files with a timestamp for an entered date period.

$file >= '2012-04-02 OR $file <= '2012-03-04'

 

 

This lists the JPG files but my modification to list files using a timestamp does not work nor can I find a way of seeing why it does not work. This has me stuck.

 

Thank you in advance to anyone who can post some pointers.

 

 

Code that lists JPGS only

 

<?php

$path = 'imgs/';


  function directory($dir,$filters)
{
    $handle=opendir($dir);
    $files=array();
    if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}  // 'all' filter is if you set to list ALL files and folders.
    if ($filters != "all")
    {
        $filters=explode(",",$filters);
        while (($file = readdir($handle))!==false)
        {
            for ($f=0; $f < sizeof($filters); $f++):
                $system=explode(".",$file);
                if ($system[1] == $filters[$f]){$files[] = $file;}
            endfor;
        }
    }
    closedir($handle);
    return $files;
} 
  
  

$files = directory($path, "jpg"); //for multiple file types just separate the file extensions by commas
foreach ($files as $value) 
{
echo $value.'<br>';
} 
?>

 

 

I have added the filter

 && filemtime($file) >= strtotime('2010-04-12')

but it does not work.

 

<?php

$path = 'imgs/';


  function directory($dir,$filters)
{
    $handle=opendir($dir);
    $files=array();
    if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}  // 'all' filter is if you set to list ALL files and folders.
    if ($filters != "all")
    {
        $filters=explode(",",$filters);
        while (($file = readdir($handle) && filemtime($file) >= strtotime('2010-04-12'))!==false)
        {
            for ($f=0; $f < sizeof($filters); $f++):
                $system=explode(".",$file);
                if ($system[1] == $filters[$f]){$files[] = $file;}
            endfor;
        }
    }
    closedir($handle);
    return $files;
} 
  
  

$files = directory($path, "jpg"); //for multiple file types just separate the file extensions by commas
foreach ($files as $value) 
{
echo $value.'<br>';
} 
?>

 

Link to comment
Share on other sites

You've got a couple of problems with your code.

 

First, you want to check and make sure $system[1] is actually set (in case you get a file in your directory that doesn't have an extension, or it runs across a directory).

 

Second, you want to take the time test out of the while statement and check for it on its own. This bit of code is just confusing the system.

 

Third, you don't want to check $system[1] for the file extension, because some files may have multiple periods in them, i.e. cat.home.jpg. Use array_pop instead to get the last index of the array, which should contain the extension.

 

Here is your code after I included all of the above changes:

 

<?php

$path = '.';


  function directory($dir,$filters)
{
    $handle=opendir($dir);
    $files=array();
    if ($filters == "all"){while(($file = readdir($handle))!==false){$files[] = $file;}}  // 'all' filter is if you set to list ALL files and folders.
    if ($filters != "all")
    {
        $filters=explode(",",$filters);
        while (($file = readdir($handle))!==false)
{
 if(filemtime($file) >= strtotime('2010-04-12')){

    for ($f=0; $f < sizeof($filters); $f++):
                $system=explode(".",$file);
                if(isset($system[1])){
                	if (array_pop($system) == $filters[$f]){$files[] = $file;}
                }
            endfor;
         } // end if(filetime...)
        } // end while($file...)
    }
    closedir($handle);
    return $files;
} 
  
  

$files = directory($path, "jpg"); //for multiple file types just separate the file extensions by commas
foreach ($files as $value) 
{
echo $value.'<br>';
} 
?>

Link to comment
Share on other sites

Thanks very much. I added $dir to the code that you re-write for me.

 

From..

if(filemtime($file) >= strtotime('2010-04-12'))

 

to..

if(filemtime($dir.$file) >= strtotime('2010-04-12'))

 

The path is now correct to read the timestamp of the files.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.