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
https://forums.phpfreaks.com/topic/183334-show-random-images/
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
https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967707
Share on other sites

i now have all my images being listed instead of just 20, with this error at the top:

 

  Quote
Warning: array_splice() expects parameter 1 to be array, boolean given in E:\xampp\htdocs\Katies Site 1.0\index.php on line 64

 

line 64 refaring to "array_splice(shuffle($files), 20);"

Link to comment
https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967725
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
https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-967730
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
https://forums.phpfreaks.com/topic/183334-show-random-images/#findComment-968070
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.