jetsettt Posted April 12, 2012 Share Posted April 12, 2012 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
batwimp Posted April 12, 2012 Share Posted April 12, 2012 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
jetsettt Posted April 12, 2012 Author Share Posted April 12, 2012 Thank you very much Batwimp for your help. I see what you mean with the timestamp check. I'm unable to get the code working but I'll have a go at getting it to work. Your help is very much appreciated. Quote Link to comment Share on other sites More sharing options...
batwimp Posted April 12, 2012 Share Posted April 12, 2012 Let us know what kind of problems you are having. The code works fine on my machine. Quote Link to comment Share on other sites More sharing options...
batwimp Posted April 12, 2012 Share Posted April 12, 2012 Oh, I forgot to tell you, I also changed the $path variable to suite my needs for testing. You will need to change that back to what you had. Quote Link to comment Share on other sites More sharing options...
jetsettt Posted April 12, 2012 Author Share Posted April 12, 2012 I saw the $path variable needed changing. I'm not getting any output. I do trust the code is good but can't seen to get it working. I have put it in place where the old code was. Quote Link to comment Share on other sites More sharing options...
batwimp Posted April 12, 2012 Share Posted April 12, 2012 Create a new directory and copy 20 or so files into there. Change the path in your code, and run it against a directory that doesn't have 100,000 files in it. Quote Link to comment Share on other sites More sharing options...
jetsettt Posted April 12, 2012 Author Share Posted April 12, 2012 Got it setup on a test file and directory. It works with '.' set as the path but not 'imgs/' set as a path (contains a small number of .jpgs) Can't figure why it won't work with a path other than root set? Quote Link to comment Share on other sites More sharing options...
batwimp Posted April 12, 2012 Share Posted April 12, 2012 Where is your images directory in relation to your php file? Quote Link to comment Share on other sites More sharing options...
jetsettt Posted April 12, 2012 Author Share Posted April 12, 2012 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. Quote Link to comment Share on other sites More sharing options...
batwimp Posted April 12, 2012 Share Posted April 12, 2012 So does it work now? Quote Link to comment Share on other sites More sharing options...
jetsettt Posted April 12, 2012 Author Share Posted April 12, 2012 Yes Batwimp. Working great thanks to you. I was stuck for a few hours and your help has got me going again. Thankyou very much. This forum is great. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.