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. Quote Link to comment 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 # Quote Link to comment 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. Quote Link to comment 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(); ?> Quote Link to comment 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 ;-) Quote Link to comment Share on other sites More sharing options...
Timma Posted June 2, 2007 Author Share Posted June 2, 2007 Alright, thanks a lot you two. Quote Link to comment 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.. Quote Link to comment 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 Quote Link to comment 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? Quote Link to comment 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++; } Quote Link to comment 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 Quote Link to comment 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] Quote Link to comment 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 Quote Link to comment 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! 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.