prakash Posted May 16, 2008 Share Posted May 16, 2008 Hi, I have one directory which will contains images having numeric values (say 1.jpg, 2.jpg .......) I wanted to get the filename of that image which is having highest numeric value within the dir. If there is 1.jpg, 2.jpg, 3.jpg, 4.jpg the file having highest numeric value will be 4.jpg is there any easy solution for this or how can I do it Quote Link to comment Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 scandir($dir,1); This should result in listing them opposite-alphabetically. Quote Link to comment Share on other sites More sharing options...
prakash Posted May 16, 2008 Author Share Posted May 16, 2008 thaks it's working Quote Link to comment Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 No problem. Quote Link to comment Share on other sites More sharing options...
prakash Posted May 16, 2008 Author Share Posted May 16, 2008 I encounter problems with scandir($dir,1); actually when I have files like 8.jpg, 9.jpg, 10.jpg, 11.jpg it actually display 9.jpg as a highest value or order. so how to resolve this issue. below is my code <?php $scanDir=scandir('images', 1); $intFile=$scanDir[0]; print $intFile; // it prints 9.jpg where it should be 11.jpg ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 16, 2008 Share Posted May 16, 2008 You could name your files 0001.jpg, 0002.jpg, etc Quote Link to comment Share on other sites More sharing options...
prakash Posted May 16, 2008 Author Share Posted May 16, 2008 You could name your files 0001.jpg, 0002.jpg, etc that may be an another option. but for using that I need the increment and decrement value for next and previous image. so how could it be possible 0004+1=0005 or 0004-1=0003 Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 16, 2008 Share Posted May 16, 2008 <?php $a = '0004'; $b = $a + 1; // $b is now 5 print str_pad($b,4,STR_PAD_LEFT).".jpg"; ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 16, 2008 Share Posted May 16, 2008 another option is to use a database to store the info Quote Link to comment Share on other sites More sharing options...
prakash Posted May 16, 2008 Author Share Posted May 16, 2008 <?php $a = '0004'; $b = $a + 1; // $b is now 5 print str_pad($b,4,STR_PAD_LEFT).".jpg"; ?> mmm just found the mistake from php.net site str_pad($b, 4, STR_PAD_LEFT).".jpg" should be str_pad($b, 4, "0", STR_PAD_LEFT).".jpg"; correct me if I am wrong and thanks for the help Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 16, 2008 Share Posted May 16, 2008 Yeah...I forgot an argument. You got it right. Quote Link to comment Share on other sites More sharing options...
prakash Posted May 16, 2008 Author Share Posted May 16, 2008 thanks it solved out my problem Quote Link to comment Share on other sites More sharing options...
947740 Posted May 16, 2008 Share Posted May 16, 2008 Sorry about that. scandir($dir,1) does it inversely alphabetically, and I forgot that it would do that... 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.