slamothe Posted January 31, 2013 Share Posted January 31, 2013 (edited) I'm trying to scan a directory and return a single specific folder,( the last one created) This is for a photo website and I would like to use images from current weeks images for the header images without having to upload them to a rotator folder. So here's where I'm at $dir = './This/2013/Goes to specific class of race/'; $files1 = scandir($dir); rsort($files1); print_r($files1); Returns all files in DESC order, including a photo.xml file generated by album builder, Array ( [0] => photos.xml [1] => 09_01_2012 [2] => 08_25_2012 [3] => 08_18_2012....ect,ect I want it to always ONLY return the NEWEST album folder, which in this example its ([1] => 09_01_2012) That's why I resorted into descending figuring the newest would always be on top of list. But I can not figure out how to JUST GET that single folder. ____________________________________________________________ Then I need to access that NEWEST folder and randomly select a .jpg file form it. If I manually insert the correct path I can get it to select a random file using this [/size][/font][/color] <?php //path to directory to scan $directory = "a/2013/direct/path/"; //get all files in specified directory $files = glob($directory . "*"); //print each file name foreach($files as $file) echo $file; } } ?> Above code with below code returns a random .jpg file, i CAN NOT GET THEM TO WORK COMBINED [color=#000000][font='Times New Roman'][size=1] [/size][/font][/color]/<?php // retrieve one of the options at random from the array $file = $files[rand(0,count($files))]; print_r($file); ?> I know very little about PHP, have done some CF coding but either way I hack my way through.... Any help or suggestions would be greatly appreciated, If you get me leaning the right direction, I can usually figure out the logic and complete it.... Hope I made sense to someone! LOL Steve Edited January 31, 2013 by slamothe Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/ Share on other sites More sharing options...
requinix Posted January 31, 2013 Share Posted January 31, 2013 If you can rely on the modification time, $dirs = glob("/path/to/folders/*", GLOB_ONLYDIR); usort($dirs, "filemtime"); $newest = end($dirs); Otherwise use usort() but with some function to compare two MM_DD_YYYY strings. Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409262 Share on other sites More sharing options...
slamothe Posted January 31, 2013 Author Share Posted January 31, 2013 (edited) Thank you requinix, I assume that's a solution JUST for getting the single file from the directory? I dropped it in and it returned: Warning: Wrong parameter count for filemtime() in /home/content/........... on line 12 Edited January 31, 2013 by slamothe Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409264 Share on other sites More sharing options...
requinix Posted January 31, 2013 Share Posted January 31, 2013 No, that's for getting the newest folder. Randomly selecting a file is as simple as getting a list of all the files and picking one. Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409267 Share on other sites More sharing options...
slamothe Posted January 31, 2013 Author Share Posted January 31, 2013 Thats what I meant, newest folder, sorry.... it returned the error I posted above? any suggestions on why? and I was able to construct the code to select the single random jpeg, I just couldn't figure out how to merge it with the folder code as a single script,, not that it really matters.. it works separately. Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409270 Share on other sites More sharing options...
requinix Posted January 31, 2013 Share Posted January 31, 2013 Eh, so you'd have to write a simple function for it instead. function($a, $B) { return filemtime($B) - filemtime($a); } pass that to usort instead of just "filemtime", the $b-$a means it'll sort in reverse order. Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409281 Share on other sites More sharing options...
slamothe Posted January 31, 2013 Author Share Posted January 31, 2013 OK, So taking it step by step and looking at out put I have a working code. What I'm wondering now is a better (more efficient) way to do it. And I say this not knowing if how I have it is inefficient. I accomplished it using 3 steps.. Step 1, ....I Called Newest folder from that particular racing division <?php $dirs = glob("./FOLDER/YEAR/DIVISION/*", GLOB_ONLYDIR); $newest = end($dirs); echo "$newest"; ?> OUTPUT:/FOLDER/2012/360 Sprints/09_01_2012 STEP 2... I pass $newest Var and add extension to sub folder to list all files in the array <?php $dir = "$newest/medium"; $files1 = scandir($dir); rsort($files1); print_r($files1); ?> OUTPUT:Array ( [0] => 09_01_2012_187.JPG [1] => 09_01_2012_186.JPG [2] => 09_01_2012_185.JPG...ect STEP 3... I passed the Array and filtered it for JPG files only, then to randomly select a single file. <?php // retrieve one of the options at random from the array //get all image files with a .jpg extension. $files = glob($files1 . "*.jpg"); $files = $files1[rand(0,count($files1))]; print_r($files); ?> OUTPUT:09_01_2012_059.JPG <!--that randomly changes on each onload--> Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409370 Share on other sites More sharing options...
requinix Posted January 31, 2013 Share Posted January 31, 2013 You're relying entirely on the filenames being sortable. They are not. When the dates start overlapping years you'll get names like 12_22_2012 12_29_2012 01_05_2013 01_12_2013 That will not sort the way you want it to. Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409373 Share on other sites More sharing options...
slamothe Posted January 31, 2013 Author Share Posted January 31, 2013 Hmm, this type of racing only runs from March to September, and file structure is always like this..... /main folder/ Year/division/Image size/Jpg's/ So I WILL have to edit the $dir-VAR annually to point it to the correct year, but I could not find a way around it with my PHP inexpierience I work on the filemtime code you suggested but ran into the issue of those not being reliable to get the the "NEWEST FILE" because the flash album builder does not upload the directories in order. I was able to call the filemtimes and review them and decided that wasn't going to work. Any other suggestions on a better way to sort? Also, Is this efficient code? Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409380 Share on other sites More sharing options...
requinix Posted January 31, 2013 Share Posted January 31, 2013 Break the filenames down into the month, day, and year components, then compare those. You can cheat by reassembling them into YYYY-MM-DD format and comparing as strings. function($a, $B) { list($am, $ad, $ay) = explode("_", basename($a)); list($bm, $bd, $by) = explode("_", basename($B)); return strcmp("{$ay}-{$am}-{$ad}", "{$by}-{$bm}-{$bd}"); } Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409386 Share on other sites More sharing options...
slamothe Posted January 31, 2013 Author Share Posted January 31, 2013 Thank You, I was hoping for Homework tonight! LOL I will give that a go, and report back I appreciate your help! Quote Link to comment https://forums.phpfreaks.com/topic/273850-selecting-a-single-directory-with-scandir-then-select-ranom-image/#findComment-1409389 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.