Timma Posted June 2, 2007 Share Posted June 2, 2007 I have some files in a folder called "file1.png", "file2.png", "file3.png", etc. I was just wondering if there was a way to find which the highest number in the file name. Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/ Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 if you put em into an array, and natsort() them... the bottom one'd be your highest # Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266882 Share on other sites More sharing options...
Timma Posted June 2, 2007 Author Share Posted June 2, 2007 Dang, any method of getting all the files in a folder into an array? Mostly because the folder gets updated, and then that would just be another thing to change every update. Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266885 Share on other sites More sharing options...
chigley Posted June 2, 2007 Share Posted June 2, 2007 <?php $filenames = array(); $dir = dir("directoryname"); while (($file = $dir->read()) != false) { $filenames[] = $file; } $dir->close(); ?> Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266887 Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 if you glob() the folder... you can easily grab all the files into an array... $files=glob('path/file*.png'); natsort($files); $high=$files[count($files)-1]; chigley - my way's faster ;-) Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266888 Share on other sites More sharing options...
Timma Posted June 2, 2007 Author Share Posted June 2, 2007 Alright, thanks a lot you two. Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266890 Share on other sites More sharing options...
chigley Posted June 2, 2007 Share Posted June 2, 2007 Gnash... never come across [pre]glob()[/pre] before.. Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266891 Share on other sites More sharing options...
Timma Posted June 2, 2007 Author Share Posted June 2, 2007 Error Time. I used the glob method, but that only returns the number 9, when I was testing with 16 files. Secondly, I need a way to remove the "images/file .png", but leave the number. So possibly just counting the amount of files in the folder and then finding the highest. Obviously I don't know how to do this or I wouldn't be saying it Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266893 Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 if you print_r($files); after the natsort... how's it sorting it? Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266894 Share on other sites More sharing options...
Timma Posted June 2, 2007 Author Share Posted June 2, 2007 Well, I found a different way to do it, after looking in the manual at glob(). foreach (glob("images/comic/comic*.jpg") as $notused) { $last++; } Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266896 Share on other sites More sharing options...
taith Posted June 2, 2007 Share Posted June 2, 2007 that is assuming that you dont skip any numbers Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266897 Share on other sites More sharing options...
Timma Posted June 2, 2007 Author Share Posted June 2, 2007 I'll try not to Also [solved] Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266898 Share on other sites More sharing options...
kathas Posted June 2, 2007 Share Posted June 2, 2007 why dont you count() the files array??? it is the same as your last post Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266905 Share on other sites More sharing options...
Timma Posted June 2, 2007 Author Share Posted June 2, 2007 Thanks, I took that into consideration. It's mega short! $last = glob("images/comic/comic*.jpg"); $last = count($last); Thanks! Link to comment https://forums.phpfreaks.com/topic/53988-solved-finding-highest-number/#findComment-266922 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.