Jump to content

show random images


jacko310592

Recommended Posts

hey guys

 

can someone please suggest a way in which the following code can be edited so it only picks up .jpg files, and so it only shows 20 of them which where found, in a randomized order

 

<?php
$files = new RecursiveDirectoryIterator("./gallery");
foreach(new RecursiveIteratorIterator($files) as $files)
{
$file = basename($files);
echo ''.$file.'<br/>';
}
?>

 

 

thanks everyone

Link to comment
Share on other sites

I'm not sure if this class implements an easier way to do this, there isn't documentation about it in the manual, but I don't think so. Something like this will work:

 

function grab_files($start_dir, $ext)
{
$dir = new RecursiveDirectoryIterator($start_dir);
foreach($dir->getChildren() as $file)
	if(is_dir($file))
		$files = array_merge((array) $files, grab_files($file, $ext));
	else
		if(pathinfo($file, PATHINFO_EXTENSION) == $ext)
			$files[] = (string) $file;
return $files;
}

$files = grab_files('../gallery', 'jpg');
array_splice(shuffle($files), 20);
foreach($files as $file)
{
echo $file . '<br />';
}

 

Edit: Added the 20 file limit and randomization, forgot you wanted that as well.

Link to comment
Share on other sites

I should stop trying to do 23940748 things at once, sorry.

 

function grab_files($start_dir, $ext)
{
$dir = new RecursiveDirectoryIterator($start_dir);
$files = Array();
foreach($dir->getChildren() as $file)
	if(is_dir($file))
		$files = array_merge($files, grab_files($file, $ext));
	else
		if(pathinfo($file, PATHINFO_EXTENSION) == $ext)
			$files[] = (string) $file;
return $files;
}

$files = grab_files('../gallery', 'jpg');
shuffle($files); array_splice($files, 20);
foreach($files as $file)
{
echo $file . '<br />';
}

 

Silly mistake.

Link to comment
Share on other sites

It would probably be easier and more correct to make proper use of the recursible nature of the RecursiveDirectoryIterator; it would save manually recursing the grab_files function and a lot of array merging.

 

Here is a version the same function with a couple of differences:

1. Recurses using the iterator rather than by calling the function multiple times

2. Does not need array_merge

3. Allows multiple file extensions and checks them case insensitively

 

The method of getting random keys is different too; instead of shuffling the entire array (which might be huge) it uses array_rand

 

function grab_files($start_dir, $ext)
{
$dir   = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($start_dir));
$files = array();

// Force array of extensions and make them all lower-case
if ( ! is_array($ext))
{
	$ext = (array) $ext;
}

$ext = array_unique(array_map('strtolower', $ext));

foreach($dir as $file)
{
	// Skip anything that isn't a file
	if ( ! $file->isFile())
		continue;

	// If the file has one of our desired extensions, add it to files array
	if (in_array(strtolower(pathinfo($file->getFilename(), PATHINFO_EXTENSION)), $ext)) {
		$files[] = $file->getPathname();
	}
}

return $files;
}

// Grab all .jpg files recursively
$files = grab_files('./gallery', 'jpg');

// Get random keys
$keys = array_rand($files, min(20, count($files)));
foreach($keys as $key)
{
echo $files[$key] . "<br>\n";
}

Link to comment
Share on other sites

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.