Jump to content

Recommended Posts

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 by slamothe

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.

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 by slamothe

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.

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.

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-->

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.

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?

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}");
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.