jasonc Posted July 1, 2009 Share Posted July 1, 2009 i have a folder with many files in it all numbered with dates. i.e. 20090701 which would be (1 July 2009) each file is a HTML file 20090701.html and i wish to find the most up-to-date file in this folder. i have this so far but thought i'd try to use a mask but this did not work Code: <? $maxfile = max(scandir($uploaddir . "articles/*.htm")); echo("'" . $maxfile . "'"); ?> reason being is that their are some other files also in this folder but i only want to have the files with .htm as the ext and not all the other files to be used in the list. how do i stop the other files from being in this list ? EDITED.... $maxfile = max(glob("articles/*.htm")); echo("'" . $maxfile . "'<br><br>"); preg_match('{(\d+-\d+-\d+)}', $maxfile, $m); IS THIS THE CORRECT WAY TO GRAB THE NUMVERS WITH THE - BETWEEN THEM ? echo $m[1]; Link to comment https://forums.phpfreaks.com/topic/164379-solved-search-a-folder-for-highest-number-file-name/ Share on other sites More sharing options...
kenrbnsn Posted July 1, 2009 Share Posted July 1, 2009 You should use the glob function. <?php $files = glob($uploaddir . 'articles/*.htm'); $maxfile = $files[count($files) - 1]; echo $maxfile; ?> Ken Link to comment https://forums.phpfreaks.com/topic/164379-solved-search-a-folder-for-highest-number-file-name/#findComment-867100 Share on other sites More sharing options...
jasonc Posted July 1, 2009 Author Share Posted July 1, 2009 yes this seems to work.. but how would i correctly grab the numbers from the file name? Link to comment https://forums.phpfreaks.com/topic/164379-solved-search-a-folder-for-highest-number-file-name/#findComment-867157 Share on other sites More sharing options...
kenrbnsn Posted July 1, 2009 Share Posted July 1, 2009 I would use pathinfo <?php $files = glob($uploaddir . 'articles/*.htm'); $maxfile = $files[count($files) - 1]; echo $maxfile . '<br>'; $dt = pathinfo[$maxfile,PATHINFO_FILENAME]; echo date('Y-m-d',strtotime($dt)); ?> Ken Link to comment https://forums.phpfreaks.com/topic/164379-solved-search-a-folder-for-highest-number-file-name/#findComment-867248 Share on other sites More sharing options...
jasonc Posted July 1, 2009 Author Share Posted July 1, 2009 I would use pathinfo <?php $files = glob($uploaddir . 'articles/*.htm'); $maxfile = $files[count($files) - 1]; echo $maxfile . '<br>'; $dt = pathinfo[$maxfile,PATHINFO_FILENAME]; echo date('Y-m-d',strtotime($dt)); ?> Ken i get the following error... Parse error: syntax error, unexpected '[' in the $dt = pathinfo[$maxfile,PATHINFO_FILENAME]; line Link to comment https://forums.phpfreaks.com/topic/164379-solved-search-a-folder-for-highest-number-file-name/#findComment-867357 Share on other sites More sharing options...
johnathanhebert Posted July 1, 2009 Share Posted July 1, 2009 You could just use the basename() function... echo basename($maxfile,".htm"); Link to comment https://forums.phpfreaks.com/topic/164379-solved-search-a-folder-for-highest-number-file-name/#findComment-867362 Share on other sites More sharing options...
jasonc Posted July 1, 2009 Author Share Posted July 1, 2009 thank you to all that have helped Link to comment https://forums.phpfreaks.com/topic/164379-solved-search-a-folder-for-highest-number-file-name/#findComment-867378 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.